簡體   English   中英

即使未安裝應用程序,canOpenURL 也會為自定義 URL 方案返回 true

[英]canOpenURL returning true for custom URL scheme even if app is not installed

 NSString *customURL = @"mycustomurl://"; 

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

即使未安裝公開自定義 URL 的目標應用程序,應用程序也會為“canOpenURL”返回 true。 此行為發生在手機和模擬器上。 openURL 然后默默地失敗。 任何想法為什么會發生這種情況/如何捕捉這種情況?

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

在此處輸入圖片說明

如果不將上述內容添加到主應用程序的 info.plist(相應地更改方案),canOpenURL 將始終返回 NO。 除非使用 iOS SDK 低於 9.0 的應用程序,否則它不會發生。

另外,請使用以下邏輯,因為它更安全:

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 字段中輸入應用程序方案 url 字符串,然后按 Enter。 從結果中得出如何進行。

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

它僅適用於LSApplicationQueriesSchemes

行不通

網址類型

這將工作

LSApplicationQueriesSchemes

這是我用來打開 Uber 應用程序(如果已安裝)的方法,否則打開 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

像這樣(應用程序 uber 和 twitter 的名稱已包含在此) info.plist 截圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM