canOpenURL:总是失败

时间:2016-10-25 09:34:12

标签: xcode swift2 nsurlrequest viber

即使我已将其添加到我的 LSApplicationQueriesSchemes

,我也总是得到此回复
<key>LSApplicationQueriesSchemes</key>
    <array>
        <string>fbapi</string>
        <string>fb-messenger-api</string>
        <string>fbauth2</string>
        <string>fbshareextension</string>
        <string>viber</string>
        <string>whatsapp</string>
    </array>

我执行我的代码

let viberUrl = NSURL( string: "viber://forward?text=Hello")!

      if UIApplication.sharedApplication().canOpenURL(viberUrl) {
        print("app installed")
      }

和xcode记录:

-canOpenURL: failed for URL: "viber://" - error: "This app is not allowed to query for scheme viber"

我可以使用我的应用程序打开viber应用程序,但我也想测试应用程序是否已安装。

任何线索我哪里出错了?或者我会错过什么? 我的测试设备iphone 5s与iOS 9。

1 个答案:

答案 0 :(得分:0)

尝试这样做

let mainViberUrl = NSURL(string: "viber://")!
let viberUrl = NSURL( string: "viber://forward?text=Hello")!

if UIApplication.sharedApplication().canOpenURL(mainViberUrl) {
    print("app installed")
    UIApplication.sharedApplication().openUrl(viberUrl)
}

我认为最好像我的代码那样做,导致forward?text=Hello" NSURL的查询部分产生错误,导致canOpenURL返回false。

注意:这适用于我的btw

这是一个可以帮助您更好地理解这一点的链接 http://useyourloaf.com/blog/querying-url-schemes-with-canopenurl/