[[UIApplication sharedApplication] canOpenURL:taobaoUrl] 打開未安裝app

當使用[[UIApplication sharedApplication] canOpenURL:taobaoUrl] 打開未app時,有時明明為安裝該app,依然會返回ture 。

被這個問題困擾了很久,經過排查發(fā)現(xiàn),是在添加白名單時出了問題:

此處以淘寶為例:

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> ????????


這時候可能還會出現(xiàn)

-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(@"---- 都打不開 ---------");
??? }

做個記錄,也希望能幫到出現(xiàn)類似困惑的小伙伴

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容