美文网首页
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

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