检查iOS 9和10上已安装的应用

时间:2017-03-07 15:15:33

标签: ios swift url swift3

我正在尝试检查用户是否安装了几个应用。我需要它在ios9和ios 10上工作。     我首先在ios9上进行测试,即使应用程序通过,schemeAvailable func也会返回true     没有安装在设备上。

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 :(得分:3)

听起来你没有正确的所有注册。您的App1和App2都需要URL标识符和方案,以及您希望启动App1和App2&#34;的应用程序。必须定义LSApplicationQueriesSchemes

E.G。

这是在&#34; App1&#34;

enter image description here

这是在&#34; App2&#34;

enter image description here

这是在&#34; LauncherApp&#34;

enter image description here

然后,在&#34; LauncherApp&#34;我能做到:

    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.")
    }

答案 1 :(得分:0)

您不必添加其他应用程序&#39;你的app plist上的方案。如果你这样做,你的应用程序将成为该方案的URL的响应者,并且始终是真的。

canOpenURL方法未在documentation

上标记为已弃用

我使用自定义方案打开应用网址的部分代码复制了一个片段...

/**
    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并显示推特网页。

@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)
}