13

I am using webview for my Swift app and I have "Share on WhatsApp" button on my website which works fine on a browser. But on iPhone app, when I click on the button, nothing happens.

How to open WhatsApp from my app? I am using Xcode 8 and iOS 10.

Neenu
  • 6,848
  • 2
  • 28
  • 54
Ninder Chand
  • 389
  • 1
  • 5
  • 15

5 Answers5

23

For Swift 4.2+ and iOS 9+

Method 1: (Launches WhatsApp app if installed)

let phoneNumber =  "+989160000000" // you need to change this number
let appURL = URL(string: "https://api.whatsapp.com/send?phone=\(phoneNumber)")!
if UIApplication.shared.canOpenURL(appURL) {
    if #available(iOS 10.0, *) {
        UIApplication.shared.open(appURL, options: [:], completionHandler: nil)
    }
    else {
        UIApplication.shared.openURL(appURL)
    }
}

Method 2:(open WhatsApp short-link web page using safari)

let phoneNumber =  "+989160000000" // you need to change this number
let appURL = URL(string: "https://wa.me/\(phoneNumber)")!
if UIApplication.shared.canOpenURL(appURL) {
    if #available(iOS 10.0, *) {
        UIApplication.shared.open(appURL, options: [:], completionHandler: nil)
    } else {
        UIApplication.shared.openURL(appURL)
    }
}

Note : '+' in phone number is OK.

You can combine both methods:

func createWhatsappURL(phoneNumber: String) -> URL {
    return URL(string: "https://api.whatsapp.com/send?phone=\(phoneNumber)")!
}

func createWebWhatsappURL(phoneNumber: String) -> URL {
    return URL(string: "https://wa.me/\(phoneNumber)")!
}

func openURL(_ url: URL) {
    if #available(iOS 10.0, *) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    } else {
        UIApplication.shared.openURL(url)
    }
}

func openWhatsapp(withPhoneNumber phoneNumber: String) {
    let appURL = createWhatsappURL(phoneNumber: phoneNumber)
    let webURL = createWebWhatsappURL(phoneNumber: phoneNumber)
    
    if UIApplication.shared.canOpenURL(appURL) {
        openURL(appURL)
    } else {
        openURL(webURL)
    }
}

// usage
let phoneNumber = "+989160000000" // you need to change this number
openWhatsapp(withPhoneNumber: phoneNumber)
Sabrina
  • 2,531
  • 1
  • 32
  • 30
16

I know this is an old question, but the following worked for me (I'm using xcode 8.3.3 and swift 3).

I added whatsapp query scheme inside Info.plist.

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

Info.plist

After adding it, the following works:

let urlString = "whatsapp://send?text=Message to share"

let urlStringEncoded = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)

let URL = NSURL(string: urlStringEncoded!)

if UIApplication.shared.canOpenURL(URL! as URL) {
    UIApplication.shared.openURL(URL! as URL)
}
  • Excellent, now from here, how do you SIMPLY attach a .pdf file to your example? Every other example I can find uses the DocumentInteractionsController. – DaWiseguy Apr 10 '18 at 00:30
  • 1
    @DaWiseguy, do you mean open a pdf file? – Leonardo Ferreira Apr 10 '18 at 22:04
  • I was under the understanding that you could also attach a pdf via the same urlString.. but after further research it looks as if you must use the DocumentInteractionController to attach any files to a WhatsApp message in Swift. – DaWiseguy Apr 10 '18 at 22:10
  • Does this open WhatsApp directly or it opens safari first? Can you put in the phone number as well? – JayVDiyk Oct 02 '20 at 16:57
  • 1
    Hi @JayVDiyk, in this case it opens WhatsApp directly if it is installed. Otherwise it won't go anywhere. You can use phone like this: whatsapp://send?phone=+55999999999 Replace 55 by the country code, and the rest by the phone number. – Leonardo Ferreira Oct 05 '20 at 21:16
15
UIApplication.shared.openURL(URL(string:"https://api.whatsapp.com/send?phone=phoneNumber")!)

phoneNumber might be with (+) or not.

phoneNumber looks like 99455555555 or +99455555555

UPDATED: + sign is needed to be removed.

ElvinM
  • 410
  • 7
  • 16
  • 3
    It works without + sign and I checked with + sign also and it still works – Akhilendra Singh Oct 28 '18 at 10:12
  • does not work for me with +, had to remove it to get Whatsapp to actually open a conversation, otherwise I get an error on Whatsapp app saying "This link couldn't be opened. Check the link and try again" – Melisa D Jun 15 '20 at 10:52
3

Devs Here is my Code for Opening WhatsApp Chat in Xcode 13.0 and iOS 15.0 for specific Contact.

func navigateToWhatsApp() {
    var countryCode = "91". //Country code
    var mobileNumber = "1234567890" //Mobile number
    let urlString = "https://api.whatsapp.com/send?phone=\(countryCode)\(mobileNumber)"

    let urlStringEncoded = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)

    let URL = NSURL(string: urlStringEncoded!)

    if UIApplication.shared.canOpenURL(URL! as URL) {
        debugPrint("opening Whatsapp")
        UIApplication.shared.open(URL as! URL, options: [:]) { status in
            debugPrint("Opened WhatsApp Chat")
        }
    } else {
        debugPrint("Can't open")
    }
}
Kudos
  • 1,224
  • 9
  • 21
1

For this you should use URL schemes.

let message = "Message"
let urlWhats = "whatsapp://send?text=\(message)"

if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) {
    if let whatsappURL = NSURL(string: urlString) {
        if UIApplication.shared.canOpenURL(whatsappURL as URL) {
            UIApplication.shared.open(whatsappURL as URL, options: [:], completionHandler: { (Bool) in

            })
        } else {
            // Handle a problem
        }
    }
} 
Oleg Gordiichuk
  • 15,240
  • 7
  • 60
  • 100
  • thanks for reply, but it did nt work, i use whatsapp share button in my website,, so just want to open from ios app – Ninder Chand Oct 02 '16 at 15:24