Sending message to WhatsApp from your app using Swift?

前端 未结 6 1283

For one of my app, I wanted to share data to WhatsApp contacts. I tried few solutions overs the StackOverflow but couldn\'t get exact solution. After some trials could achie

6条回答
  •  离开以前
    2020-12-13 21:21

    Swift 3.0

    Try with this code for access watsapp in your application. Its working perfectly for me.

    @IBAction func sendButtonAction(_ sender: Any)
    {
        let date = Date()
        let msg = "Hi my dear friends\(date)"
        let urlWhats = "whatsapp://send?text=\(msg)"
    
        if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) {
            if let whatsappURL = NSURL(string: urlString) {
                if UIApplication.shared.canOpenURL(whatsappURL as URL) {
                    UIApplication.shared.openURL(whatsappURL as URL)
                } else {
                    print("please install watsapp")
                }
            }
        }
    }
    

提交回复
热议问题