How to open WhatsApp from Swift app?

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

    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.

    LSApplicationQueriesSchemes
    
        whatsapp
    
    

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

提交回复
热议问题