Tony Stark
Tony Stark

Reputation: 451

Open Whatsapp on a particular number in swift

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

Upvotes: 24

Views: 22825

Answers (7)

kapKr21
kapKr21

Reputation: 29

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

Upvotes: 2

Farid Blaster
Farid Blaster

Reputation: 984

This issue really helpful

just put people number without "+" example: 60161234567

Upvotes: 1

Milind Chaudhary
Milind Chaudhary

Reputation: 1740

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: [:])
    }

Upvotes: 5

Hardik Thakkar
Hardik Thakkar

Reputation: 16001

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>

Upvotes: 31

Sheereen S
Sheereen S

Reputation: 1236

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>

Upvotes: 54

Bharat Bapodara
Bharat Bapodara

Reputation: 86

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

Upvotes: -3

Rajat
Rajat

Reputation: 11127

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.

Upvotes: 1

Related Questions