app之间跳转_URL Schemes

前言:
1.告诉别人怎么跳到自己这边(设置schemes)
2.告诉自己可以跳转到那个人那边(ios9之后 才有这个 设置白名单)
3.自己发出跳转
4.别人接受跳转消息

背景:例子以两个app__ schemesA schemesB 配置完成 两个app在测试机运行起来

告诉别人怎么跳到自己这边(设置schemes)

schemesA

app之间跳转_URL Schemes_第1张图片
SchemesA 设置URL Schemes

schemesB
app之间跳转_URL Schemes_第2张图片
SchemesB 设置URL Schemes

告诉自己可以跳转到那个人那边(ios9之后 才有这个 设置白名单 LSApplicationQueriesSchemes) 配合canOpenURL:使用

info.plist 进行配置
SchemesA 粘贴进info.plist

LSApplicationQueriesSchemes
    
        SchemesA
    

SchemesB 粘贴进info.plist

LSApplicationQueriesSchemes
    
        SchemesB
    
SchemesA 发出跳转
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    //有参数 跳转到SchemeB
    NSString *paramStr = [NSString stringWithFormat:@"SchemesB://username=%@/password=%@",@"username-schemesB",@"password-schemesB"];
    NSURL *schemesUrl = [NSURL URLWithString:[paramStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
#ifdef __IPHONE_10_0  //ios10及以后
    if ([[UIApplication sharedApplication] canOpenURL:schemesUrl]) {
        [[UIApplication sharedApplication] openURL:schemesUrl options:@{UIApplicationOpenURLOptionUniversalLinksOnly : @NO} completionHandler:nil];
//NO 跳转    YES 不跳转
    }
#else
    if ([[UIApplication sharedApplication] canOpenURL:schemesUrl]) {
        [[UIApplication sharedApplication] openURL:schemesUrl];
    }
#endif
}
//如果不设置白名单 canOpenURL:  通过这个方法 就找不到对应的app  
//但是直接进行调用 openURL: 不先判断 还是可以跳转到对应app
SchemesB 接受跳转消息

状态一:SchemesB 杀死状态 重新启动 会调用下面方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    if (launchOptions && launchOptions.count) {
        NSString *schemesUrlStr = [launchOptions.allValues.lastObject absoluteString];
        //获取URL中的参数
        [self getUsernameAndPasswordWithSchemesUrlStr:schemesUrlStr];
    }
    return YES;
}
//截取username password 自定义
- (void)getUsernameAndPasswordWithSchemesUrlStr:(NSString *)schemesUrlStr {
    ViewController *vc = (ViewController *)self.window.rootViewController;
    
    NSString *resultStr = [schemesUrlStr stringByReplacingOccurrencesOfString:@"SchemesB://" withString:@""];
    NSArray *arrStr = [resultStr componentsSeparatedByString:@"/"];
    //username
    NSArray *usernameArr = [arrStr[0] componentsSeparatedByString:@"="];
    vc.username = usernameArr.lastObject;
    
    //password
    NSArray *passwordArr = [arrStr[1] componentsSeparatedByString:@"="];
    vc.password = passwordArr.lastObject;
}

状态二:SchemesB 后台运行 会调用下面方法

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options {
    NSString *schemesUrlStr = [url absoluteString];
    [self getUsernameAndPasswordWithSchemesUrlStr:schemesUrlStr];
    return NO;
}
//截取username password 自定义
- (void)getUsernameAndPasswordWithSchemesUrlStr:(NSString *)schemesUrlStr {
    ViewController *vc = (ViewController *)self.window.rootViewController;
    
    NSString *resultStr = [schemesUrlStr stringByReplacingOccurrencesOfString:@"SchemesB://" withString:@""];
    NSArray *arrStr = [resultStr componentsSeparatedByString:@"/"];
    //username
    NSArray *usernameArr = [arrStr[0] componentsSeparatedByString:@"="];
    vc.username = usernameArr.lastObject;
    
    //password
    NSArray *passwordArr = [arrStr[1] componentsSeparatedByString:@"="];
    vc.password = passwordArr.lastObject;
}

推荐:https://sspai.com/post/31500

你可能感兴趣的:(app之间跳转_URL Schemes)