Introduction to the

Every APP in iPhone has a sandbox. APP is a single entity with independent information and cannot communicate with each other. However, iOS apps can register their own URL Scheme, which is designed to facilitate the call between apps. We can open the APP through the openURL of the system and pass some parameters. The URL Scheme must uniquely represent an APP. If the URL Scheme you set conflicts with the URL Scheme of other apps, your APP may not be launched. Because when your APP is installed, your URL Scheme has already been registered in the system. Generally, the APP installed first will be called. However, the URL Scheme of iOS system APP is definitely the highest, so when defining the URL Scheme, we should try to avoid the URL Scheme already defined by the system APP

The URL Scheme name should only contain English characters and cannot contain underscores or numbers

Register the URL Scheme

Methods a

Go to TARGETS -> Info -> URL Types and click Add

Method 2

Right – click in info.plist and selectAdd Row option, and then enter URL types of type Array

The URL Identifier is the name of a user-defined URL scheme. Generally, the unique name is ensured by reversing the domain name, such as com.demob. WWW. However, to open an application in iOS, you only need to get the protocol header of the application (URL Scheme). So we just need to configure the application’s protocol header. An app can have multiple URL Schemes

use

1. Switch from Application A to application B

Create two applications: DemoA and DemoB, DemoB registered Scheme as “DemoBScheme”, the following implementation DemoA→DemoB jump

#pragma mark - DemoA -> DemoB - (IBAction)jumpToDemoB:(id)sender { NSString *urlString = @"DemoBScheme://"; // No argument NSURL *url = [NSURL URLWithString:urlString]; if ([[UIApplication sharedApplication] canOpenURL:url]) { [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) { }]; } else {[self showMessage:@" no app "]; }}Copy the code

Run the two apps separately, and then click the corresponding button in DemoA to jump to DemoB, you will find that the jump is not possible, because after iOS9, if you use canOpenURL: method, the URL Scheme involved in the change method must be whitelisted in info.plist, otherwise it cannot be used

In the info. Add LSApplicationQueriesSchemes plist fields, the field is an array type, and then add the key value DemoBScheme (DemoB Scheme)Then run and click the button to jump to demoB

2. The page is displayed

Sometimes we need to jump to a specific page of an app, such as sharing to moments page

  1. First create PageOne and PageTwo pages in DemoB

In the click event of DemoA, we can change urlString to DemoBScheme://page1, where DemoBScheme:// is the scheme applied by DemoB, and page1 is the identifier to jump to PageOne page agreed with DemoB

#pragma mark - jumpToPageOne page - (IBAction)jumpToPageOne (id)sender {// DemoBScheme: is the scheme of the DemoB application Page1 is the identifier to jump to PageOne page as agreed with DemoB. In DemoB, the delimiter intercepts DemoA's Scheme. DemoAScheme is its own scheme. NSString *urlString = @"DemoBScheme://page1? DemoAScheme"; NSURL *url = [NSURL URLWithString:urlString]; if ([[UIApplication sharedApplication] canOpenURL:url]) { [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) { }]; } else {[self showMessage:@" no app "]; }}Copy the code
  1. Implement Application (UIApplication *)app openURL (NSURL *) URL in AppDelegateTe of DemoB Options: (NSDictionary options > < UIApplicationOpenURLOptionsKey, id *) method
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options { // 1. To obtain navigation controller UINavigationController * rootNav = (UINavigationController *) self. Window. The rootViewController; / / 2. Get the master controller UIViewController * mainVc = [rootNav. ChildViewControllers firstObject]; / / 3. Must be in before every jump with controller (detail) [rootNav popToRootViewControllerAnimated: NO]; If ([url.absoluteString containsString:@"page1"]) {PageOneViewController *page = [[PageOneViewController] alloc] init]; page.urlString = url.absoluteString; [mainVc.navigationController pushViewController:page animated:YES]; } else if ([url.absoluteString containsString:@"page2"]) { PageOneViewController *page = [[PageOneViewController alloc] init]; page.urlString = url.absoluteString; [mainVc.navigationController pushViewController:page animated:YES]; } else {UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@" open "message:[NSString stringWithFormat:@"scheme - %@,\n host -- %@,\n query -- %@",url.scheme,url.host,url.query] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; NSLog(@"query -- %@", url.query); [alertView show]; } return YES; }Copy the code

This enables DemoA to jump to a specific page in DemoB

– (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation method set

3. Return from DemoB to DemoA

The above mentioned jump from DemoA to DemoB, sometimes need to jump back to the original app after the jump, for example, after the completion of sharing, the choice will be returned to the original app or stay in wechat

  1. To jump from one application to another, you need to register the URL Scheme for DemoA and whitelist DemoB’s info.plist to add DemoA’s Scheme
  2. When we jump from DemoA to DemoB, we should pass in DemoA’s Scheme
#pragma mark - jumpToPageOne page - (IBAction)jumpToPageOne (id)sender { You should also pass in URL Schemes that apply A. So we can decide which application to jump back to. Protocol header :// apply URL Schemes of B? Use URL Schemes for A. Namely: AppB: / / Page1? AppA. // Is DemoB's scheme page1 the identifier used by DemoB to jump to PageOne? In DemoB, the delimiter intercepts DemoA's Scheme. DemoAScheme is its own scheme. NSString *urlString = @"DemoBScheme://page1? DemoAScheme"; // DemoBScheme: NSURL *url = [NSURL URLWithString:urlString]; if ([[UIApplication sharedApplication] canOpenURL:url]) { [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) { }]; } else {[self showMessage:@" no app "]; }}Copy the code
  1. In DemoB – (BOOL)application:(UIApplication *)app openURL:(NSURL *)url Options: (NSDictionary < UIApplicationOpenURLOptionsKey, id > *) in the options method, we give PageOne received the url of the page
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options { // 1. To obtain navigation controller UINavigationController * rootNav = (UINavigationController *) self. Window. The rootViewController; / / 2. Get the master controller UIViewController * mainVc = [rootNav. ChildViewControllers firstObject]; / / 3. Must be in before every jump with controller (detail) [rootNav popToRootViewControllerAnimated: NO]; If ([url.absoluteString containsString:@"page1"]) {PageOneViewController *page = [[PageOneViewController] alloc] init]; page.urlString = url.absoluteString; [mainVc.navigationController pushViewController:page animated:YES]; } else if ([url.absoluteString containsString:@"page2"]) { PageOneViewController *page = [[PageOneViewController alloc] init]; page.urlString = url.absoluteString; [mainVc.navigationController pushViewController:page animated:YES]; } else {UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@" open "message:[NSString stringWithFormat:@"scheme - %@,\n host -- %@,\n query -- %@",url.scheme,url.host,url.query] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; NSLog(@"query -- %@", url.query); [alertView show]; } return YES; }Copy the code
  1. Intercept DemoA’s Scheme through the agreed cut symbol in the click event of DemoB’s pageOne page
#pragma mark - return DemoA - (IBAction)backDemoA:(id)sender {// 1. Get the URL of the corresponding application Scheme through the agreed separator, okay? Cutting nsstrings * urlSchemeString = [[self urlString componentsSeparatedByString: @ "? "] lastObject]; NSString *urlString = [urlSchemeString stringByAppendingString:@"://"]; NSURL * URL = [NSURL URLWithString:urlString]; If ([[UIApplication sharedApplication] canOpenURL:url]) {[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil]; }}Copy the code

4. Send parameters through the URL

Sometimes when we jump to another APP, we need to pass some parameters, and let the other APP make corresponding behaviors according to the parameters we pass

Parameter format:

“DemoBScheme://? Name =lwy&phone=110” is the same as the get request

  1. The parameter is passed during the jump
#pragma mark - DemoA -> DemoB - (IBAction)jumpToDemoB:(id)sender { // NSString *urlString = @"DemoBScheme://"; NSString *urlString = @"DemoBScheme://? name=lwy&phone=110"; NSURL *url = [NSURL URLWithString:urlString]; if ([[UIApplication sharedApplication] canOpenURL:url]) { [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) { }]; } else {[self showMessage:@" no app "]; }}Copy the code
  1. In demoB – (BOOL)application:(UIApplication *)app openURL:(NSURL *)url Options: (NSDictionary options > < UIApplicationOpenURLOptionsKey, id *) method can get parameters
 NSString *query = url.query;
Copy the code

5. Open the APP via the url

Web page opening app is also based on the protocol header of APP (URL Scheme) to distinguish which app is opened. We directly copy and paste our URL scheme on the browser, and the system will automatically pop up a box to remind us whether to open this app, which is the same as the jump from DemoA to DemoB. Of course, the webpage parameter is the same format as the APP redirect parameter