【问题标题】:Check for installed apps on iOS 9 and 10检查 iOS 9 和 10 上已安装的应用程序
【发布时间】:2017-07-27 20:56:46
【问题描述】:

我正在尝试检查用户是否安装了几个应用程序。我需要它在 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 种 url 类型并为每种类型插入了标识符和 url 方案。

【问题讨论】:

  • @dmorrow 我在解释 ios9 的一个问题
  • canOpenURL 未被弃用
  • 你能分享一下实际的 url 方案吗?不是 app1app2
  • @dmorrow com.companyname.app1, com.companyname.app2。它们尚未在商店中发布。它们是本地应用程序

标签: ios swift url swift3


【解决方案1】:

听起来您的所有注册都不正确。您的 App1 和 App2 都需要 URL 标识符和方案,并且“想要启动 App1 和 App2”的应用程序必须定义 LSApplicationQueriesSchemes

例如

这是在“App1”中

这是在“App2”中

这是在“LauncherApp”中

然后,在“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.")
    }

【讨论】:

    【解决方案2】:

    您不必在您的应用列表中添加其他应用的方案。如果您这样做,您的应用程序将成为该方案的 URL 的响应者,并且始终为 true。

    canOpenURL 方法在 documentation 上未标记为已弃用

    我复制了一个 sn-p,其中包含我用来通过自定义方案打开应用程序 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)
    }
    

    【讨论】:

    • 如果我在项目的信息选项卡中删除了 2 个 url 类型、标识符和 url 方案,那么我的 URL 失败:“app1://” - 错误:“(null)”并且失败了URL:“app2://” - 错误:“(null)”,即使安装了 app2
    • 嗨@user2363025。我已经添加了一个示例。
    • 这个答案令人困惑。这些方案必须在LSApplicationQueriesSchemes 下列出。
    • 必须为您自己的应用列出方案(您自己的方案)。但没有注册外部方案,如 twitter、facebook...@rmaddy
    • @Adolfo 这是不正确的。与 canOpenURL 一起使用的任何 URL 方案都必须列在 LSApplicationQueriesSchemes 下。这是自 iOS 9 以来的要求。
    猜你喜欢
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    • 2011-03-15
    • 2021-04-02
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    相关资源
    最近更新 更多