24

I am trying to open a particular contact chat in whatsapp but not getting any solution. Please help i am totally stuck. I have tried this:

let whatsAppURL: NSURL = NSURL(string: "whatsapp://send?abid=\(primary)&;text=lOL;")!
        if UIApplication.sharedApplication().canOpenURL(whatsAppURL){
            UIApplication.sharedApplication().openURL(whatsAppURL)
        }
Tony Stark
  • 451
  • 1
  • 3
  • 11

7 Answers7

54

Its possible You can send messages to Specfic user.

Direct app chat url open

let urlWhats = "whatsapp://send?phone=+919789384445&abid=12354&text=Hello"
    if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) {
        if let whatsappURL = URL(string: urlString) {
            if UIApplication.shared.canOpenURL(whatsappURL!) {
                UIApplication.shared.openURL(whatsappURL!)
            } else {
                print("Install Whatsapp")
            }
        }
    }

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

WebUrl Link open Chat

 let whatsappURL = URL(string: "https://api.whatsapp.com/send?phone=9512347895&text=Invitation")
    if UIApplication.shared.canOpenURL(whatsappURL) {
        UIApplication.shared.open(whatsappURL, options: [:], completionHandler: nil)
    }

Check below link,

https://www.whatsapp.com/faq/en/general/26000030

Note: Add url scheme in info.plist

<key>LSApplicationQueriesSchemes</key>
 <array>
    <string>whatsapp</string>
 </array>
Anuran Barman
  • 1,556
  • 2
  • 16
  • 31
Sheereen S
  • 1,292
  • 10
  • 18
31

For swift 4.2 / Swift 5

func openWhatsapp(){
    let urlWhats = "whatsapp://send?phone=(mobile number with country code)"
    if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: CharacterSet.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 {
                    UIApplication.shared.openURL(whatsappURL)
                }
            }
            else {
                print("Install Whatsapp")
            }
        }
    }
}

For Swift 2.0

let urlWhats = "whatsapp://send?phone=(mobile number with country code)"
        if let urlString = urlWhats.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()){
            if let whatsappURL = NSURL(string: urlString) {
                if UIApplication.sharedApplication().canOpenURL(whatsappURL){
                    UIApplication.sharedApplication().openURL(whatsappURL)
                }
                else {
                    print("Install Whatsapp")
                }
            }
        }

Note: Add url scheme in info.plist

<key>LSApplicationQueriesSchemes</key>
 <array>
    <string>whatsapp</string>
 </array>
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
  • 1
    This solution worked but I would like to know why adding url scheme, because the solution still works without url scheme in info.plist – Biasi Wiga May 21 '20 at 08:08
5

This worked neatly for me

    if let url = URL(string: "https://wa.me/91xxxxxxxxxx?text=Hello"),
            UIApplication.shared.canOpenURL(url) {
                UIApplication.shared.open(url, options: [:])
    }
Milind Chaudhary
  • 1,632
  • 1
  • 17
  • 16
2
  1. Add attributes to the Info.plist source code file like this:

    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>whatsapp</string>
    </array>
    
  2. Use the following function on custom click in your app:

    func openWhatsapp(){
        let urlWhats = "https://wa.me/numberWithCountryCode"
        if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: CharacterSet.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 {
                        UIApplication.shared.openURL(whatsappURL)
                    }
                } else {
                    Alert(withMessage: "You do not have WhatsApp installed! \nPlease install first.").show(andCloseAfter: 2.0)
                }
            }
        }
    }
    
fcdt
  • 2,371
  • 5
  • 14
  • 26
kapKr21
  • 29
  • 1
1

As per this whatsapp forum link, there is no way you can send message to specific user, this is not available within whatsapp URL scheme.

You just set predefined message and then with URL scheme you are able to open whatsapp recent controller.

Rajat
  • 10,977
  • 3
  • 38
  • 55
  • 2
    Sheereen S's answer (https://stackoverflow.com/a/45351187/1898973) is correct. Sending a Whatsapp to a particular phone number, even if it is not a contact in the address book, is completely possible. – Leandro Fournier Oct 23 '17 at 13:33
  • It's possible https://faq.whatsapp.com/en/general/26000030 by Sheereen S – Yahya Alshaar Jan 31 '18 at 06:45
  • This is possible. By @HardikThakkar answer i am able to open chat in whatsapp for a given number – Mujahid Latif Aug 06 '19 at 06:22
  • Downvoted the answer because it is not true anymore, go to https://www.whatsapp.com/faq/en/iphone/23559013 and read it. @Rajat – Boris Nikolic Jan 15 '21 at 14:33
1

This issue really helpful

just put people number without "+" example: 60161234567

Farid Blaster
  • 974
  • 1
  • 9
  • 23
-3

This is not possible you can just open WhatsApp with URL scheme.