How to open WhatsApp from Swift app?

前端 未结 5 1659
无人共我
无人共我 2021-02-03 15:51

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 butt

5条回答
  •  盖世英雄少女心
    2021-02-03 16:09

    For Swift 4.2+ and iOS 9+

    Method 1: (launches WhatsApp application if is 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)
        }
    } else {
        // WhatsApp is not installed
    }
    

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

    Method 3: if WhatsApp is installed launch app, otherwise open short-link in safari.(combine method 1 and 2)

    Note : '+' in phone number is OK.

提交回复
热议问题