How to open WhatsApp from Swift app?

前端 未结 5 1664
无人共我
无人共我 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:21

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

提交回复
热议问题