Sending message to WhatsApp from your app using Swift?

前端 未结 6 1287

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:08

    Swift 5

    Code

    let urlWhats = "whatsapp://send?text=\("Hello World")"
    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)
                 } 
                 else {
                     print("please install watsapp")
                 }
          }
    }
    

提交回复
热议问题