How to open WhatsApp from Swift app?

怎甘沉沦 提交于 2019-12-21 02:55: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.


回答1:


UIApplication.shared.openURL(URL(string:"https://api.whatsapp.com/send?phone=phoneNumber")!)

phoneNumber is not with (+).

phoneNumber looks like 99455555555




回答2:


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



回答3:


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)




回答4:


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



回答5:


Here is a simple answer I use in a switch structure. This loads whatsApp. I am still looking for a way to call a specific contact.

case "whatsApp":
        let usefullWhere: String = "whatsapp://?app"//
        let url = NSURL(string: usefullWhere)!
        UIApplication.sharedApplication().openURL(url)


来源:https://stackoverflow.com/questions/39809620/how-to-open-whatsapp-from-swift-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!