当使用[[UIApplication sharedApplication] canOpenURL:taobaoUrl] 打开未app时,有时明明为安装该app,依然会返回ture 。
被这个问题困扰了很久,经过排查发现,是在添加白名单时出了问题:
此处以淘宝为例:
1、添加CFBundleURLTypes(URL types)
必须CFBundleURLTypes 添加item 值为taobao:// 注意必须为英文
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string></string>
<key>CFBundleURLSchemes</key>
<array>
<string>taobao://</string>
</array>
</dict>
2、添加 LSApplicationQueriesSchemes
必须LSApplicationQueriesSchemes 添加item 值为taobao:// 注意必须为英文
<key> LSApplicationQueriesSchemes</key>
<array> <string>taobao</string></array>
这时候可能还会出现
-canOpenURL: failed for URL: "taobao://" - error: " The operation couldn’t be completed.(OSStatus error -10814.)”
此打印log可忽略,原因是要验证的app未安装到手机上,安装了便不会报错。
打开淘宝时根据是否安装判断跳转原生还是webView:
NSString *url = model.clickUrl;//接口返回的跳转链接
NSURL *taobaoUrl = [NSURL URLWithString:@"taobao://"];
if ([[UIApplication sharedApplication] canOpenURL:taobaoUrl]) {
NSString * result = nil;
if ([url hasPrefix:@"https"]) {//若以https开头择去除https
NSRange range = [url rangeOfString:@"https"];
result = [url substringFromIndex:range.length];
}else if ([url hasPrefix:@"http"]) {//若以http开头择去除http
NSRange range = [url rangeOfString:@"http"];
result = [url substringFromIndex:range.length];
}
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"taobao%@",result]]];
HGJLog(@"---- 不能打开淘宝 ---------");
} else {
//都打不开就自己加载网页
WebViewController *activityWebVC = [[WebViewController alloc] init];
activityWebVC.url = url;
[self.navigationController pushViewController:activityWebVC animated:YES];
HGJLog(@"---- 都打不开 ---------");
}
做个记录,也希望能帮到出现类似困惑的小伙伴
网友评论