4

I'm trying to send a whatsapp message to a recipient number stored in a global variable!

By using this simple code:

let whatsAppUrl = NSURL(string: "whatsapp:\(globalPhone)")
            
if UIApplication.shared.canOpenURL(whatsAppUrl as! URL) {
    UIApplication.shared.openURL(whatsAppUrl as! URL)
} else {
    let errorAlert = UIAlertView(
        title: "Sorry",
        message: "You can't send a message to this number",
        delegate: self,
        cancelButtonTitle:"Ok"
    )
    errorAlert.show()
}

I'm always getting the alert message which's the else case! although the number is always true! May be the the error in the url syntax?

In the console:

canOpenURL: failed for URL: "whatsapp:0534260282" -
"This app is not allowed to query for scheme whatsapp"

Is this the correct way to do that? Or this way just for sharing, text through Whatsapp?

Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
Mariah
  • 573
  • 3
  • 10
  • 26

3 Answers3

13

Try this....

let urlWhats = "whatsapp://send?phone=***********&text=***"

var characterSet = CharacterSet.urlQueryAllowed
characterSet.insert(charactersIn: "?&")
  
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: characterSet){
                   
if let whatsappURL = NSURL(string: urlString) {
    if UIApplication.shared.canOpenURL(whatsappURL as URL) {
        UIApplication.shared.openURL(whatsappURL as URL)
    } else {
        print("Install Whatsapp")
                           
    }
}

Note:Country code (Ex:+91) is mandatory to open mobile number Chat

Note: Add url scheme in info.plist

<key>LSApplicationQueriesSchemes</key>
   <array>
  <string>whatsapp</string>
</array>
Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
Sreekanth
  • 549
  • 5
  • 13
5

Two issues.

The first is that's not a valid url scheme. A URL scheme takes the format identifier://params so you'll need to use whatsapp://phone_number instead.

Secondly is that Apple now requires you to define which external url schemes that your application uses in your Info.plist file, nested under the key LSApplicationQueriesSchemes. Please see iOS 9 not opening Instagram app with URL SCHEME for more info.


According to the Whatsapp URL scheme docs, you can't actually supply the phone number of the contact that you'd like to send the message to: https://www.whatsapp.com/faq/en/iphone/23559013.

You can however supply the message that you'd like to send to them:

whatsapp://send?text=Some%20Text.

Ensure that the text is percentage encoded as otherwise NSURL will fail to create a valid URL from the supplied string.

Community
  • 1
  • 1
max_
  • 24,076
  • 39
  • 122
  • 211
1

If you want to send to a specific number use the folowing code:

let whatsAppUrl = NSURL(string: "https://api.whatsapp.com/send?phone=**********&text=******")
    
if UIApplication.shared.canOpenURL(whatsAppUrl as! URL) {
    UIApplication.shared.openURL(whatsAppUrl as! URL)
} else {
    let errorAlert = UIAlertView(
        title: "Sorry",
        message: "You can't send a message to this number",
        delegate: self,
        cancelButtonTitle:"Ok"
    )
    errorAlert.show()
}

The trick works because you don't call directly whatsapp, but the browser, and this one can receive the number of phone and open whatsapp to desired phone and desired text

Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
christian mini
  • 1,662
  • 20
  • 39
  • Can i send a single message to more than one number – shubham mishra Sep 10 '19 at 13:57
  • @shubhammishra AS far I now is not possible, but you can simply iterate foreach phone number by sending the same message – christian mini Sep 11 '19 at 09:20
  • Thanks bro, but I do have to handle the next iteration call on the success of the first one since, Calling openURL() inside for loop will leads to open WhatsApp multiple times and will end up by opening chat screen for the last number. So, basically can I have closure or any kind of callbacks after openURL(). If u find the solution please add it into your answer. – shubham mishra Sep 13 '19 at 06:46
  • this is not working anymore, whatsapp open and show error "The link couldn't be opened check the link and try again." – Waseem Sarwar Apr 19 '21 at 10:55