2015-12-08 32 views
1
NSString *customURL = @"mycustomurl://"; 

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:customURL]]) { 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:customURL]]; 
} else { 
    ... 
} 

即使未安裝公開自定義URL的目標應用程序,該應用程序也會爲'canOpenURL'返回true。這種行爲發生在兩個手機&模擬器上。 openURL然後默默地失敗。任何想法爲什麼發生這種情況/如何捕捉這種情況?即使沒有安裝應用程序,canOpenURL也會爲自定義URL方案返回true

+0

參閱本教程[在iOS/iPhone自定義URL方案的完整教程](http://iosdevelopertips.com/cocoa/launching-your-own-application-via- a-custom-url-scheme.html),它可以幫助你。或可能重複[啓動另一個應用程序(iPhone)](http://stackoverflow.com/questions/419119/launch-an-app-from-within-another-iphone) –

回答

1

如果在SDK 9.0及更高版本中使用應用程序,則必須確保在主應用程序的信息中添加要打開的應用程序方案。 plist:

enter image description here

沒有添加上述的主應用程序的Info.plist(變化方案相應)canOpenURL將始終返回NO。除非使用低於9.0的iOS SDK的應用程序,否則不會發生。

此外,使用下面的邏輯,因爲它是更安全:

NSString * urlStr = @"mycustomurl://"; 
NSURL * url = [NSURL URLWithString:urlStr]; 

if ([[UIApplication sharedApplication] canOpenURL:url]) { 
    if([[UIApplication sharedApplication] openURL:url]) { 
     // App opened 
    } else { 
     // App not opened 
    } 
} else { 
    // Can not open URL 
} 

最後檢查我的建議是在設備打開Safari瀏覽器應用程序,請在網址領域的應用方案的URL字符串,然後按ENTER鍵。從結果中總結如何繼續。

+0

感謝您的答覆,LSApplicationQueriesScheme指定在應用程序。輸入不同於其中指定的URL將導致canOpenURL爲「否」。是的,目標應用方案先前已安裝在SIM卡和手機上。 –

+0

@stuart_gunn嘗試重新啓動設備,然後再次嘗試。 –

+1

@stuart_gunn如果您在設備中打開Safari瀏覽器應用程序,然後在url字段(appscheme://)中輸入應用程序方案url字符串,請按回車,它是否會打開應用程序? –

-1

這是我用來打開Uber應用,如果其他開放尤伯杯網站

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"uber://"]]) 
{ 
    //Uber is installed 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"uber://"]]; 
} 
else 
{ 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://uber.com"]]; 
} 

不要忘了在你的Info.plist文件

像這樣添加此LSApplicationQueriesSchemes(安裝它應用程序超級和微博的名稱已包含在此)info.plist screenshot

0

確保您使用的不是LSApplicationQueriesSchemes URL類型的

它運作良好,只爲LSApplicationQueriesSchemes

這將不行

URL types

這將工作

LSApplicationQueriesSchemes

相關問題