Solutions for opening other access applications in the SDK

Source: Internet
Author: User

The solution for opening other connected applications in the SDK has never been allowed to open another application in the program during iOS development. Later, some scholars used class-dump to find such a function in private APIs. That is, use launchApplicationWithIdentifier: suincluded: Of the UIApplication to open it. Use the following method: NSString * identifier = [[NSBundle mainBundle] objectForInfoDictionaryKey: @ "CFBundleIdentifier"]; [[UIApplication sharedApplication] launchApplicationWithIdentifier: identifier suincluded: NO]; after all, private APIs are not a good method. At least you will never be recognized by App Store. In some cases, we may still need such a feature. As an SDK, there is still a better solution. That is, use the openURL: Method of UIApplication. Let's take a look at the openURL and implementation solutions. OpenURL is actually a rich set of functions. In addition to simply calling safari to open a website, you can also use google map search, Mail, call, send text messages, and open the AppStore. -(IBAction) openMaps {// open a map // Where is Apple on the map anyway? NSString * addressText = @ "1 Infinite Loop, Cupertino, CA 95014"; // URL encode the spaces addressText = [addressText Syntax: parameters]; NSString * urlText = [NSString stringWithFormat: @ "http://maps.google.com/maps? Q = % @ ", addressText]; // lets throw this text on the log so we can view the url in the event we have an issue NSLog (urlText ); [[UIApplication sharedApplication] openURL: [NSURL URLWithString: urlText];}-(IBAction) openEmail {// open mail // Fire off an email to apple support [[UIApplication sharedApplication] openURL: [NSURL URLWithString: @ "mailto: // devprograms@apple.com"];} -(IBAction) openPhone {// call // Call Google 411 [[UIApplication sharedApplication] openURL: [NSURL URLWithString: @ "tel: // 8004664411"];}-(IBAction) openSms {// open SMS // Text to Google sms [[[UIApplication sharedApplication] openURL: [NSURL URLWithString: @ "SMS: // 466453"];}-(IBAction) openBrowser {// open the browser // Lanuch any iPhone developers fav site [[UIApplication sharedApplication] openURL: [NSURL URLWithString: @ "http: // itunesconn Ect.apple.com "];} How to open other applications from an application is actually very simple. Open info. plist, add a URL types, expand URL types, expand Item1, change the URL identifier under Item1 to URL Scheme, and expand URL Scheme, modify the content of Item1 to myapp. Other programs can access this custom URL through myapp. It is similar to the following style. In this way, as long as the custom url of the application is opened, the system can help us find and open the program. NSURL * url = [NSURL URLWithString: @ "myapp:"]; [[UIApplication sharedApplication] openURL: url]; as an SDK, the advantage is that, each connected application has an AppId for differentiation, so we can make full use of this AppId. We can ask third-party developers to find the required information in their Info. configure this field in Plist so that we can open the application corresponding to AppId in our SDK interface. Of course, this requires installation of this program on the device. For example, if an application assigns an AppId of 111122223333, we need it to define the URL Schemes as NDSDK111122223333 in Info. plist. In this way, we can accurately identify whether such a program exists in the internal code. Furthermore, we can use the canOpenURL method to determine whether the application is installed on the device. If YES is returned, such a program should be installed, there should be no problem in ipa or Pxl programs. If we really choose to do this, we need to make it clear in the document. However, it should be noted that, as a programmer, he may not like reading documents very much. Maybe he did not see the documents he wrote with great effort. At this time, we should take a tough approach, so we have the following code features. 1: Check whether AppId2 is configured. If the CFBundleURLSchemes Field 3 of Info is correctly configured, can it be opened correctly. // Check App ID: // This is really a warning for the developer, this shoshould not // happen in a completed app if (! KAppId) {UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "Setup Error" message: @ "Missing app ID. you cannot run the app until you provide this in the code. "delegate: self cancelButtonTitle: @" OK "otherButtonTitles: nil, nil]; [alertView show]; [alertView release];} else {// Now check that the URL scheme fb [app_id]: // authorize is in. plist and can // be opened, doing a simple check Without local app id factored in here NSString * url = [NSString stringWithFormat: @ "fb % @: // authorize", kAppId]; BOOL bSchemeInPlist = NO; // find out if the sceme is in the plist file. NSArray * dependencies = [[NSBundle mainBundle] objectForInfoDictionaryKey: @ "CFBundleURLTypes"]; if ([aBundleURLTypes isKindOfClass: [NSArray class] & ([aBundleURLTypes count]> 0 )) {NSDictionary * aBundleURLTypes 0 = [aBundleURLTypes objectAtIndex: 0]; if ([aBundleURLTypes0 isKindOfClass: [NSDictionary class]) {NSArray * aBundleURLSchemes = [Your objectForKey: @ "CFBundleURLSchemes"]; if ([aBundleURLSchemes isKindOfClass: [NSArray class] & ([aBundleURLSchemes count]> 0) {NSString * scheme = [aBundleURLSchemes objectAtIndex: 0]; if ([scheme isKindOfClass: [NSString class] & [url hasPrefix: scheme ]) {BSchemeInPlist = YES ;}}// Check if the authorization callback will work BOOL bCanOpenUrl = [[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString: url]; if (! BSchemeInPlist |! BCanOpenUrl) {www.2cto.com UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "Setup Error" message: @ "Invalid or missing URL scheme. you cannot run the app until you set up a valid URL scheme in your. plist. "delegate: self cancelButtonTitle: @" OK "otherButtonTitles: nil, nil]; [alertView show]; [alertView release];}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.