Ios: one app starts another app, and ios starts another app
Problem description: You need to be able to start another ios application from one ios application.
Development Environment: xcode7.3.1
Keywords:Whitelist(LSApplicationQueriesSchemes ),Register your own URL
Demo source code: http://pan.baidu.com/s/1bo327sb
How can I start another app from one app ??? I am a ios cainiao (I want to learn about ios at the beginning, I want to learn how to sell it now, haha). I have implemented this function by referring to several articles on the Internet. Currently, the demo is shared.
1. register your own URL
Create a project MyAppOne, add a field to the info. plist file, and register the URL of MyAppOne.
1.1 In the Information Property List, click "+" to add the URL types, add an Item, add "URL Schemes" to the Item, and thenIn the Item of the URL Schemes, add a string "AppDemo1" (refer)The string is the URL registered by MyAppOne for the current project. Registering your own URL is complete.
1.2 The URL we just registered is equivalent to an ID of MyAppOne in our current project. Other apps use the interfaces provided by ios (which function is ios will be introduced later), and call this URL to start our MyAppOne application.
2. whitelist LSApplicationQueriesSchemes
After ios9, you must add LSApplicationQueriesSchemes (whitelist) to the info. plist file to start the app you want to start. The URL of the app you want to launch is added to the whitelist (this URL is the "register your own URL" described above ").
2.1 Add LSApplicationQueriesSchemes. (The current project is xcode7.3.1, and xcode does not automatically match LSApplicationQueriesSchemes. Therefore, you need to manually enter this field and change its attribute to [Array ]), add "AppDemo2" to LSApplicationQueriesSchemes ".
Suppose there is already a MyAppTwo, and the registered URL is AppDemo2. To start the MyAppTwo app in the MyAppOne project, you need to add the MyAppTwo URL to the whitelist.
3. key code for MyAppOne to start another App
The ": //" following the 3.1 string AppDemo2 must be added. Otherwise, the URL of your specified app cannot be opened.
3.2 The Code introduces no parameter URL and any parameter URL. If you want to pass the parameters, add them after.
1 //// The user presses the button *********, please refer to the code in the ViewController.m file
2-(IBAction) Btn1Click: (id) sender {
3 NSLog (@ "MyAppOne: You click the button1111.");
4 char szbuff [50] = {0};
5 sprintf (szbuff, "% s", "AppDemo2: // userName = heliang & pwd = 20160812");
6 /// 1. Passing method without parameters
7 // NSURL * urlLocal = [NSURL URLWithString: @ "AppDemo2: //"];
8 /// 2. Transfer method with parameters
9 NSURL * urlLocal = [NSURL URLWithString: [NSString stringWithUTF8String: szbuff]];
10
11 // Determine whether the url can be started
12 if ([[UIApplication sharedApplication] canOpenURL: urlLocal])
13 {
14 [[UIApplication sharedApplication] openURL: urlLocal];
15 NSLog (@ "launch AppDemo2 success!");
16}
17 else
18 {
19 NSLog (@ "No such url.");
20 /// Jump to an app in appStore
21 NSURL * urlAppStore = [NSURL URLWithString: @ "https://itunes.apple.com/cn/app/tie-lu12306/id564818797?mt=8"];
22 [[UIApplication sharedApplication] openURL: urlAppStore];
twenty three }
twenty four }
3.3 now, we have introduced how to start another app2 from one app1.
The procedure is as follows::
①App2In info. plist'sURL SchemesRegister your own URL
② App1 Add the URL registered with app2 to the [whitelist] of info. plist.
③ Call [UIApplication sharedApplication] openURL: urlLocal] In app1; Start app2. (You can refer to the demo in the link for further understanding .)
How can I get the parameters passed through this startup method?
4. Get the passed parameters
4.1 re-write the following application method in AppDelegate. m (if this function is not available in the file, manually add the following function, which is rewritten ). The passed parameters are in the url. (You can refer to the demo in the link for further understanding .)
4.2 do not believe in books as well as have no books. This article is not an authoritative explanation. This example is only for beginners to learn about ios. The principle and application at a deeper level require further consideration. At present, you can implement this function in your own project, and you will be successful. Congratulations!
1 ////////// Rewritten application method ****** Please refer to the code in the AppDelegate.m file
2-(bool) application: (UIApplication *) application openURL: (nonnull NSURL *) url options: (nonnull NSDictionary <NSString *, id> *) options
3 {
4 //// Print the parameters we received and the applicationkey of the application
5 NSLog (@ "AppDemo1 is started, the following are the startup parameters passed by AppDemo2:");
6 NSString * strUrl = [[NSString alloc] init];
7 strUrl = url.absoluteString;
8 NSLog (@ "strURL:% @", strUrl);
9
10 NSString * sourceApplication = options [UIApplicationOpenURLOptionsSourceApplicationKey];
11 NSLog (@ "sourceApplication:% @", sourceApplication);
12 return YES;
13}
Reference link:
Http://www.jianshu.com/p/e14df92cb207
Http://lysongzi.com/2016/03/13/iOS-%E4%BD%BF%E7%94%A8URL-Scheme%E5% AE %9E%E7%8E%B0app%E4%B9%8B%E9%97%B4%E7%9A%84%E9%80%9A%E4%BF%A1/