Open another APP: URL Scheme, openURL, schemeopenurl
Target
We usually do iOS development and often encounter the function of opening other apps. This article is about opening others' apps. Our goal is:
Open others' apps
Let others open our APP
Adaptation of iOS9
Use URL Schemes to transmit data
Preparations
Create a workspace named OpenApp to store two of our projects. This workspace is mainly used to allow the two projects created later to be managed on an Xcode page, easy to explain and manage. Create a workspace to store our projects
Create an iOS project named MyApp. This MyApp is "My app", which is used to open another APP. Add the project to the OpenApp. xcworkspace we just created. Add a button in the Main. storyboard of the project. We will use it to write the method later.
MyApp Project
Create an iOS project named WXApp. This project is a simulated "APP", which is opened by a person. To distinguish two applications, we add a label "I am an App" to the Main. storyboard ".
Create WXApp project WXApp in workspace
Now, the preparation is so easy.
Open others' apps and let others open our apps
To open someone else's APP or let someone else open our APP, you need to use URL Schemes.
What is URL Schemes?
URL Schemes is a mechanism provided by Apple to jump to a system application or to another application. Data can also be transferred between applications.
Comparing web links to understand the URL Schemes on iOS should be much easier. URL Schemes has two words:
URL, which is also called a URL or URL;
Schemes indicates a location in a URL-the initial location, that is, the character before. Based on the use of URL Schemes, we can easily understand that on iOS, which is dominated by local applications, we can find a Web page, use a special URL to locate a specific function of an application or even an application. To locate this application, it should be the Schemes part of the application URL, that is, the opening part.
Set a URL Schemes on the WXApp
To enable other apps (including the MyApp we just created) to open WXApp, we need to add a URL Schemes for the WXApp. Step: Select WXApp project-> Info-> URL Types-> click "+"-> Fill in the URL Schemes Columnweixin
Add a URL Schemes
Note: An application can have multiple URL Schemes. You can click "+" again to add a URL Schemes
Let's take a look at what is in the info. plist file.
URL Schemes in the info. plist File
Run WXApp. (Note which target is your run target)
Run WXApp in the simulator
In this way, WXApp registers a URL Schemes with the system. Other applications canOpenurl: method to open WXApp.
MyApp open WXApp
Now we open WXApp in MyApp. The method is very simple. Add a method to ViewController
-(IBAction) openWXApp :( UIButton *) sender {[self demo1];}-(void) demo1 {// create a url, which is the url of the WXApp. Remember to add: // NSURL * url = [NSURL URLWithString: @ "weixin: //"]; // open the url [[UIApplication sharedApplication] openURL: url];}
Run MyApp
run MyApp
After running, click "open" button."MyApp" wants to open the "WXApp" prompt box. Click OK to jump to the WXApp.
Click "open" button
WXApp Enabled
After iOS9, after an application jumps to another application, a button is displayed in the upper left corner to return to the previous application. In this way, we click "open" in MyApp, jump to WXApp, click "Back to MyApp", and return to MyApp. If you are bored, you can click the two buttons repeatedly to jump between the two applications. Haha.
It is worth noting that this URL Schemes is not unique. That is to say, the URL Schemes set between multiple applications can be the same.
The problem arises. If the two applications share the same URL Schemes, useOpenURL: Which application will the method open?
The landlord tried it on his mobile phone.
Steps:
Install MyApp on your mobile phone and click "open" button.
Then, install the WXApp on your mobile phone. Click "open" button of MyApp again. The result is WXApp.
Conclusion: if the two applications have the same URL Schemes, the URL Schemes of the later installed application overwrites the URL Schems of the earlier installed application.
Open WXApp in safari
Yes. If you have registered the URL Schemes application, you can use the safari browser to open it. I often use this to verify whether the application has set the desired URL Schemes.
Open WXApp in safari and enter it in the address bar of safari.Weixin: //, enter to open
Open WXApp with safari
Open WXApp with safari
Adaptation in iOS9
Configure the URL Schemes White List
In fact, when opening the WXApp, we should first useCanOpenURL: Determine whether the url can be opened first, and then useOpenURL method to open the URL. This provides a better user experience. Because you may not have installed WXApp. If you have not installed the tool, clicking "open" button does not respond. At this time, we should first determine whether the url can be opened (that is, whether the user has installed WXApp). If it has not been installed, we will give a warm reminder, for example, "u iv No. 4 sprinkling, how can I enable WXApp without installing it! ".
More importantly, if the click fails, it may be rejected by Apple.
-(IBAction) openWXApp :( UIButton *) sender {// [self demo1]; [self demo2];} // judge before opening WXApp-(void) demo2 {// create a url, which is the url of the WXApp. Remember to add: // NSURL * url = [NSURL URLWithString: @ "weixin: //"]; // first determine whether the url can be opened if ([[UIApplication sharedApplication] canOpenURL: url]) {// open the url [[UIApplication sharedApplication] openURL: url];} else {// give a prompt or do something else NSLog (@ "U four no four sprinkling, did not install WXApp, how to open it! ");}
But we found thatCanOpenURL: After the method, WXApp is not opened as we imagine. The Xcode console prompts:
Xcode console error message
Why?
Because Apple's permissions are enhanced during iOS9, it can only be used if the URL Schemes White List is added to the info. plist file.CanOpenURL: method to determine whether the url can be opened. The maximum number of white lists is 50. That is to say, you can only useCanOpenURL: method used to determine 50 URLs (Schemes. Of course, we usually don't use that much. Even if we integrate the sharing function, 50 will certainly be enough.
Note: OnlyCanOpenURL: there are restrictions on methods,OpenURL: there is no limit on the method.
To put it bluntly, we need to addSet weixin to whitelist.
Step: Right-click info. plist and choose Open As> Source Code> Add the following Code.
LSApplicationQueriesSchemes weixin
In this way, you can.
Use URL Schems to pass data
In addition to opening an APP, URL Schemes can also be used to transmit a small amount of data between two apps.
If you search for "ios" on Baidu, a url is generated. The following describes the composition of the url.
Https is the protocol, that is, scheme
Www.baidu.com is a domain name
/S is the path
? Query is followed by query parameters. This url has two parameters:Ie = UTF-8 andwd=ios
Our iOS URL Schemes is similar.
In additionIn openURL, if the url contains parameters, as long as the URL Schemes is correct, the App can be opened, and the following parameters will be taken to the App we opened.
Let's make a Demo.
In MyApp, write a demo3 method. The url isweixin://www.shixueqian.com/abc?title=hello&content=helloworld
-(IBAction) openWXApp :( UIButton *) sender {// [self demo1]; // [self demo2]; [self demo3];} // use URL Schemes to pass data-(void) demo3 {// create a url, which is the url of the WXApp. Remember to add: // NSURL * url = [NSURL URLWithString: @ "weixin: // www.shixueqian.com/abc? Title = hello & content = helloworld "]; // open the url [[UIApplication sharedApplication] openURL: url];}
In AppDelegate. m of WXAppApplication: openURL :( NSURL *) url sourceApplication: annotation: callback
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { NSLog(@"url=====%@ \n sourceApplication=======%@ \n annotation======%@", url, sourceApplication, annotation); return YES; }
After running, we found that we can still clickOpenURL: Open WXApp. When the WXApp is openedApplication: openURL :( NSURL *) url sourceApplication: annotation: callback method. In this callback method, we can obtain the url and other information transmitted by MyApp.
The console is printed as follows:
Log result
The complete url information is passed over, so we can use the path, parameters, and other information in the url. What do we do. In this way, data is transmitted from MyApp to WXApp.
Note:
Apple gave three openURL callbacks.
They are:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url NS_DEPRECATED_IOS(2_0, 9_0, "Please use application:openURL:options:") __TVOS_PROHIBITED; - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation NS_DEPRECATED_IOS(4_2, 9_0, "Please use application:openURL:options:") __TVOS_PROHIBITED; - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options NS_AVAILABLE_IOS(9_0); // no equiv. notification. return NO if the application can't open for some reason
Why are there three? What are the differences between the Three callbacks? (For the purpose, set ABC3 callbacks respectively)
The three callback functions are basically the same. They are executed when someone else opens an application through URL Schemes.
Differences:
A callback was introduced at iOS2.0, with only Parametersurl。
B is released at iOS4.2. The parameters include:url sourceApplication annotation.
C callback was introduced when iOS9.0 was used. The parameters include:UrlOptions.Options has the following keys:
// Keys for application:openURL:options: UIKIT_EXTERN NSString *const UIApplicationOpenURLOptionsSourceApplicationKey NS_AVAILABLE_IOS(9_0); // value is an NSString containing the bundle ID of the originating application UIKIT_EXTERN NSString *const UIApplicationOpenURLOptionsAnnotationKey NS_AVAILABLE_IOS(9_0); // value is a property-list typed object corresponding to what the originating application passed in UIDocumentInteractionController's annotation property UIKIT_EXTERN NSString *const UIApplicationOpenURLOptionsOpenInPlaceKey NS_AVAILABLE_IOS(9_0); // value is a bool NSNumber, set to YES if the file needs to be copied before use
These Callbacks have priority values. C> B>. That is to say, if all three Callbacks are implemented, the program will only execute the C callback. Other Callbacks are not executed. (Of course, only B callback is executed under iOS9 ).