美文网首页
2019-11-07 Scheme跳转运用

2019-11-07 Scheme跳转运用

作者: zxh123456 | 来源:发表于2019-11-07 17:22 被阅读0次

业务开发用到其他app跳转自己app到指定页面,实现如下:

  1. 在URL TYPES配置好scheme,约定好协议结构,如:scheme://key=vlaue

  2. 网页或者其他app点击协议scheme://key=vlaue跳转目标app
    2.1 app跳转需要在其他app的info.plist添加LSApplicationQueriesSchemes白名单,否则会报错:This app is not allowed to query for scheme XXXX

    image.png
    image.png

3.目标app实现

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //app杀死状态下,用过scheme跳转过来的解析
    NSURL *url = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey] ;
    NSString *str = [url absoluteString];
    if ([str hasPrefix:@"scheme"]) {
        NSRange range = [str rangeOfString:@"key"];
        NSString *value = [str substringFromIndex:range.location + range.length + 1];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"%@",value);
        });
    }
    ..........
    return YES;
}

#pragma mark - app在后台,被scheme调起的方法
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
        NSString *scheme = [url scheme];
        NSString *str = [url absoluteString];
    if ([str hasPrefix:@"scheme"]) {
        NSRange range = [str rangeOfString:@"key"];
        NSString *value = [str substringFromIndex:range.location + range.length + 1];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"%@",value);
        });
    }
        return YES;
}

相关文章

网友评论

      本文标题:2019-11-07 Scheme跳转运用

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