美文网首页
app之间跳转_URL Schemes

app之间跳转_URL Schemes

作者: ghost__ | 来源:发表于2017-03-15 11:33 被阅读175次

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

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

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

schemesA

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

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

<key>LSApplicationQueriesSchemes</key>
    <array>
        <string>SchemesA</string>
    </array>

SchemesB 粘贴进info.plist

<key>LSApplicationQueriesSchemes</key>
    <array>
        <string>SchemesB</string>
    </array>
SchemesA 发出跳转
- (void)touchesBegan:(NSSet<UITouch *> *)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<UIApplicationOpenURLOptionsKey,id> *)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

    前言:1.告诉别人怎么跳到自己这边(设置schemes)2.告诉自己可以跳转到那个人那边(ios9之后 才有这个 ...

  • iOS开发之App之间的跳转

    一.设置需要跳转到的App的URL Scheme 设置该App的URL Schemes 二.配置要跳转App的跳转...

  • iOS URL Schemes 系统App清单

    苹果系统自带App跳转URL Schemes ,欢迎大家补充 1.AMSEngagementViewService...

  • OpenUrl

    iOS-使用URL Schemes,进行应用之间跳转iOS - JLRoutes路由跳转官方文档JLRoutes ...

  • URL Schemes 实现APP之间的跳转

    简述 通过对比网页链接来理解iOS上的URL Schemes,应该就容易多了URL Schemes 有两个单词: ...

  • 不同应用间的跳转

    mark: iOS中scheme详解 iOS-使用URL Schemes,进行应用之间跳转 iOS 设置URL S...

  • iOS URL Schemes实现APP之间的跳转

    一、应用场景 我们的APP有时候想直接去到设置页面,程序退到后台进入设置页面然后又重新打开,这样子总是感觉多做了很...

  • APP调用本机第三方APP

    什么是URL Schemes? URL Schemes是苹果给出的用来跳转到系统应用或者跳转到别人的应用的一种机制...

  • iOS URL Types和LSApplicationQueri

    一、踩坑反思 起因:调试跳转第三方App时,无法跳转。 过程:在URL Types中添加Schemes进行模拟跳转...

  • ios10设置跳转

    跳转到WIFI : info.plist 文件 URL type -> url schemes 添加 item ...

网友评论

      本文标题:app之间跳转_URL Schemes

      本文链接:https://www.haomeiwen.com/subject/yxklnttx.html