您的位置:首页 > 移动开发

iOS 配置app打开第三方app,分享基础

2015-11-22 22:21 661 查看
目标:ios在当前app内打开其他app

说明:如果一个第三方的app想要让其他app打开其就要配置所需要的协议头url scheme (info.plist中)

1. 在当前app中添加如下方法(有直接打开app,如果没有安装则通过网页打开)

- (IBAction)jumpAction:(id)sender {
    
    NSString *telStr = @"udid://test";  // 则需要打开的app就要配置这个协议
    NSURL *url = [NSURL URLWithString:telStr];
    UIApplication *app = [UIApplication sharedApplication];
    BOOL canOpen = [app canOpenURL:url];
    if (canOpen) { // 有安装app
        BOOL isOpen = [app openURL:url];
        NSLog(@"打开了app%d",isOpen);
    } else { // 没有安装app
        NSURL *newUrl = [self replaceSchemToHttps:url];
        [app openURL:newUrl];
        // 使用浏览器打开url
    }
}

- (NSURL *)replaceSchemToHttps:(NSURL *)url {
    
    NSString *urlStr = [url absoluteString];
    NSString *newUrlStr = @"";
    NSRange range = [urlStr rangeOfString:@"//"];
    
    if (range.location == NSNotFound) return nil;
    
    newUrlStr = [urlStr substringFromIndex:range.location + range.length];
    newUrlStr = [NSString stringWithFormat:@"https://%@",newUrlStr];
    
    NSURL *newUrl = [NSURL URLWithString:newUrlStr];
    return newUrl;
}


2. 被打开的第三方app在收到 [app openURL]信息时便会得到一些处理(如果有参数便会在这个方法中进行处理)

//- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { // 只要下面的方法使用了,便不会再度调用这个方法,下个方法就是为了替代当前方法而来的
//    NSLog(@"调用些方法%s",__FUNCTION__);
//    return NO; // no 也可以打开,有问题
//}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { // 从其他app内打开本app会调用本app的此方法
    NSLog(@"调用了方法%s",__FUNCTION__);
    return NO;
}


3. 当前的情况在ios9之下都可以很轻松的应对,但到了ios9系统时便不可进行处理,需要进行一些配置

在info.plist中添加如下属性:

LSApplicationQueriesSchemes Array
	Item 0 	string udid  // udid为第三方可以打开的协议名


4. 对于第三方如果想让别的app可以打开,则需要在info.plist中添加URL types属性 【可去target info中查找】

URL Types

添加只需要注重URL Schemes即可 // 如果我当前的协议头设置为udid则我只需要将udid填写其中即可

5. 如果使用模拟器,可能会报错:【补充12.24】

LaunchServices: ERROR: There is no registered handler for URL scheme xxx


容易让人误解的是总以为自己缺少哪些东西没有配置,极有可能的问题是因为模拟器并未安装XXX所以才会导致。换成真机测试下即可

拓展阅读:http://www.jianshu.com/p/9fc9fd89bfee
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: