當使用[[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(@"---- 都打不開 ---------");
??? }
做個記錄,也希望能幫到出現類似困惑的小伙伴