smartcoderx
smartcoderx

Reputation: 1081

LSApplicationQueriesSchemes and derived data

I would like to open a whatsapp url in my app like.

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

I extend my info.plist with a dictionary "LSApplicationQueriesSchemes" and add my url scheme for whatsapp.

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

If i run my app i get the following error message.

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

I read some solutions with cleaning the derived data and run the app again to fix this issue. But this not help me, exists an other solution for my issue?

Upvotes: 7

Views: 25483

Answers (2)

Sajjon
Sajjon

Reputation: 9907

You have made the LSApplicationQueriesSchemes a dict, it must be an array, like this, then it will work :).

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

And I would also recommend you to not unwrap the optional URL using !, you can do it like this instead:

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

Upvotes: 15

Muhammad Noman
Muhammad Noman

Reputation: 1576

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

and define query scheme like that

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

Upvotes: 9

Related Questions