2017-03-07 24 views
1

我正在檢查用戶是否安裝了幾個應用程序。我需要它在ios9和ios 10上工作。 我在ios9上測試,schemeAvailable func返回true,即使應用程序通過 沒有安裝在設備上。檢查在iOS 9和10上安裝的應用程序

func schemeAvailable(scheme: String) -> Bool { 
    if let url = URL(string: scheme) { 
     return UIApplication.shared.canOpenURL(url) 
    } 
    return false 
} 

我加入以下到我的info.plist即應用我會檢查安裝:

<key>LSApplicationQueriesSchemes</key> 
<array> 
<string>app1</string> 
<string>app2</string> 
</array> 

,並在我的項目的信息標籤,我加入了2種網址類型和插入的標識和每個網址的方案。

+0

的可能的複製[的OpenURL:棄用iOS的10](http://stackoverflow.com/questions/39548010/openurl-deprecated-in-ios-10) – dmorrow

+0

@dmorrow我正在解釋ios9的問題 – user2363025

+0

'canOpenURL'不被棄用 – dan

回答

3

這聽起來像你沒有所有註冊正確。您的App1和App2都需要URL標識符和方案,並且您的「想要啓動App1和App2」的應用程序必須定義LSApplicationQueriesSchemes

E.G.

這是在 「應用1」

enter image description here

這是在 「應用2」

enter image description here

這是 「LauncherApp」

enter image description here

然後,在 「LauncherApp」 我可以這樣做:

let s = "DMCApp1://TestMe" 
    let customURL: URL = URL(string: s)! 

    if UIApplication.shared.canOpenURL(customURL) { 
     print("YES - I can open App1 !!!") 
     UIApplication.shared.open(customURL, options: [:], completionHandler: nil) 
    } else { 
     print("NO - App1 is not available.") 
    } 
0

您不必在應用程序中添加其他應用程序的方案。如果你這樣做,你的應用程序將成爲該方案的URL響應者,並且始終如此。沒有標記

canOpenURL方法上documentation

爲廢棄我複製一個片段與我使用通過定製方案打開程序URL代碼的一部分...

/** 
    Abre los perfiles de la app en las distintas redes sociales. 

    - Parameters: 
     - url: Esquema personalizado de la app 
     - secondaryURL: Si la app no está presente en el dispositivo abrimos su version web (twitter, facebook, etc...) 
*/  
    func open(_ url: String, secondaryURL: String? = nil) -> Void 
     { 
      if let url = URL(string: url), UIApplication.shared.canOpenURL(url) 
      { 
       UIApplication.shared.open(url, completionHandler: nil) 
      } 
      else if let secondaryURL = secondaryURL, let url = URL(string: secondaryURL), UIApplication.shared.canOpenURL(url) 
      { 
       UIApplication.shared.open(url, completionHandler: nil) 
      } 
      else 
      { 
       print("No se puede hacer nada de nada.\r\napp_url: \(url)") 
      } 
     } 

下面是一個例子。我嘗試打開twitter應用程序,如果未顯示,請打開Safari並顯示twitter網頁。

@IBAction private func handleTwitterButtonTap(sender: UIButton) -> Void 
{ 
    let twitter_app_url: String = "twitter://user?screen_name=GetPomodoroApp" 
    let twitter_web_url: String = "https://twitter.com/GetPomodoroApp" 

    self.open(twitter_app_url, secondaryURL: twitter_web_url) 
} 
+0

如果我刪除我的項目的信息選項卡中的2個url類型,標識符和url方案, 然後我得到 URL失敗:「app1://」 - 錯誤:「(null)」AND URL失敗:「app2 ://「 - error:」(null)「 即使安裝了app2 – user2363025

+0

Hi @ user2363025。我已經添加了一個例子。 – Adolfo

+0

這個答案很混亂。這些方案必須在「LSApplicationQueriesSchemes」下列出。 – rmaddy

相關問題