6

I know there is scheme to communicate with whatsapp like:

NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=Hello%2C%20World!"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
    [[UIApplication sharedApplication] openURL: whatsappURL];
}

But i can't find how to open whatsapp with a phone number that don't exist before.

It's for a "contatc us" page and if i can only open whatsapp and can't pre fill a phone number, it's useless. I need to be contacted with whatsapp...

Does what I am trying to do exist ?

Sahil Kapoor
  • 11,183
  • 13
  • 64
  • 87
Eric Trikk
  • 101
  • 1
  • 2
  • 6

7 Answers7

10

For phone number pre-filled And Text

   NSURL *whatsappURL = [NSURL URLWithString:@"https://api.whatsapp.com/send?phone=9530670491&text=hello"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
    [[UIApplication sharedApplication] openURL: whatsappURL];
}

click here for Swift

Check Link For More Details https://www.whatsapp.com/faq/en/general/26000030

1

According to the Whatsapp documentation, you need to actually have the contact in your address book to open a discussion from the url scheme.
So for your question

Does what i try to do exist ?

the answer is: No.

enter image description here

Quentin Hayot
  • 7,786
  • 6
  • 45
  • 62
1

You can find the documentation of that whatsapp API above answer. Adding some more point in that based on my own trial :

For communicating direct with the specific contact you need abid of that contact. ABID is that parameter which is automatically generated by the system when you save the contact. So if you dont have number saved in your contact so no way you can open that from your application .

There is one workaround that I have used. When your application load first time in the device , you save the contact us number to the address book . when number saved successfully you will get ABID in the return . you can use that ABID to invoke messaging with that contact.

NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?abid=123&text=Hello%2C%20World!"];
V-Xtreme
  • 7,230
  • 9
  • 39
  • 79
  • "whatsapp://send" method don't work stable for me. For some iOS and WhatsApp version it don't work. I recommend to use "https://wa.me". It works in the same way, opens WhatsApp app. – Argus Jun 16 '20 at 14:38
1

If anyone looking for Swift version

static func triggerWhats(_ vc:UIViewController){
        let msg = "Hello! I have a question"
        let urlWhats = "whatsapp://send?phone=+6599999999&text=\(msg)"
        if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed){
            if let whatsappURL = NSURL(string: urlString) {
                if UIApplication.shared.canOpenURL(whatsappURL as URL) {
                    _ = UIApplication.shared.open(whatsappURL as URL, options: [:], completionHandler: nil)
                } else {
                    // Cannot open whatsapp
                    ViewUtil().popAlertView(vc, title: "WhatsApp Support", message: "Please WhatsApp us at +65999999 if you have any questions", option: "Ok")
                }
            }
        }
    }
spaceMonkey
  • 4,475
  • 4
  • 26
  • 34
  • "whatsapp://send" method don't work stable for me. For some iOS and WhatsApp version it don't work. I recommend to use "https://wa.me". It works in the same way, opens WhatsApp app. – Argus Jun 16 '20 at 14:38
1

Swift 3.0 open whats app.

in info.plist file add this line

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

func OpenWhatsApp(_ vc:UIViewController){
        let msg = "" // here pass your message 
        let urlWhats = "https://api.whatsapp.com/send?phone=(Mobileno)&text=\(msg)" // Mobile number that include country code and without + sign . 
        if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed){
            if let whatsappURL = URL(string: urlString) {
                if UIApplication.shared.canOpenURL(whatsappURL) {
                    if #available(iOS 10.0, *) {
                        _ = UIApplication.shared.open(whatsappURL, options: [:], completionHandler: nil)
                    } else {
                        // Fallback on earlier versions
                        UIApplication.shared.canOpenURL(whatsappURL)
                    }
                } else {
                    // Cannot open whatsapp
                    AppUtilities.sharedInstance.showAlert(title: "WhatsApp Support", msg: "Your device is not support whats app")
                }
            }
        }
    }
Himanshu Moradiya
  • 4,769
  • 4
  • 25
  • 49
0

For Swift 3.0 OR Above, This will open whatsapp chat of a number

Example of Number: "+918798766554"

if let whatappURL = URL(string: "https://api.whatsapp.com/send?phone=\(number)&text=\(msg)"),
  UIApplication.shared.canOpenURL(whatappURL)
{
  if #available(iOS 10.0, *) {
    UIApplication.shared.open(whatappURL, options: [:], completionHandler: nil)
  } else {
    UIApplication.shared.openURL(whatappURL)
  }
}
Community
  • 1
  • 1
Hiren Panchal
  • 2,963
  • 1
  • 25
  • 21
0

According to the Whatsapp documentation

For Swift 3.0 OR Above, This will open whatsapp chat of a number

Example of Number: "+6599999999"

UIApplication.shared.open(URL(string: "https://wa.me/+6599999999")!)
Argus
  • 2,241
  • 1
  • 22
  • 27