LSApplicationQueriesSchemes和派生数据

时间:2016-07-15 10:01:07

标签: swift

我想在我的应用中打开一个whatsapp网址,如。

let whatsAppUrl = NSURL(string: "whatsapp://send?text=Hello%2C%20World!")
if UIApplication.sharedApplication().canOpenURL(whatsAppUrl!) {
    UIApplication.sharedApplication().openURL(whatsAppUrl!)
}

我使用字典“LSApplicationQueriesSchemes”扩展我的info.plist并为whatsapp添加我的url方案。

<key>LSApplicationQueriesSchemes</key>
<dict>
    <key>Item 0</key>
    <string>whatsapp</string>
</dict>

如果我运行我的应用程序,我会收到以下错误消息。

"This app is not allowed to query for scheme whatsapp"

我阅读了一些清理衍生数据的解决方案,然后再次运行应用来解决此问题。但这对我没有帮助,对我的问题存在另一种解决方案吗?

2 个答案:

答案 0 :(得分:5)

您已将LSApplicationQueriesSchemes设为dict必须是数组,如this,然后它将起作用:)。

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>whatsapp</string>
</array>

我还建议您不要使用!打开可选网址​​,您可以这样做:

guard 
   let whatsAppUrl = URL(string: "whatsapp://send?text=Hello%2C%20World!"),
   case let application = UIApplication.shared,
    application.canOpenURL(whatsAppUrl) 
else { return }
application.openURL(whatsAppUrl)

答案 1 :(得分:4)

let url = "whatsapp://send?text=Hello World!"
 if let urlString = url.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()) {
  if let whatsappURL = NSURL(string: urlString) {
   if UIApplication.sharedApplication().canOpenURL(whatsappURL) {
       UIApplication.sharedApplication().openURL(whatsappURL)
        } 
       }}

并定义类似

的查询方案
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>whatsapp</string>
</array>