Sending message to WhatsApp from your app using Swift?

陌路散爱 提交于 2019-12-17 23:25:14

问题


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 achieve what I was looking for, so sharing here for anyone's future reference.


回答1:


 var url  = NSURL(string: "whatsapp://send?text=Hello%20Friends%2C%20Sharing%20some%20data%20here...%20!")

//Text which will be shared on WhatsApp is: "Hello Friends, Sharing some data here... !"

    if UIApplication.sharedApplication().canOpenURL(url!) {
        UIApplication.sharedApplication().open(url as URL, options: [:]) { (success) in
                if success {
                    print("WhatsApp accessed successfully")
                } else {
                    print("Error accessing WhatsApp")
                }
            }
    }

Note: text needs to be URL encoded. You can get it using any of the open source tools over internet or using addingPercentEncoding(withAllowedCharacters:) function in iOS. e.g.

var urlString = "Hello Friends, Sharing some data here... !"
var urlStringEncoded = urlString.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
var url  = NSURL(string: "whatsapp://send?text=\(urlStringEncoded!)")



回答2:


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



回答3:


Addition to above solutions, starting from iOS 9, we need to add whatsapp to LSApplicationQueriesSchemes key in info.plist. After this only it worked for me.




回答4:


My code is Looking Like this

let encodeQuizStr = "Check Out The Quiz With link \n http://www.proprofs.com "

        let urlQuizStringEncoded = encodeQuizStr.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)

        guard let whatsAppUrl = NSURL(string: "whatsapp://send?text="+urlQuizStringEncoded!) else { return }

        if UIApplication.shared.canOpenURL(whatsAppUrl as URL) {

            if #available(iOS 10.0, *) {
                print(urlQuizStringEncoded!)
                UIApplication.shared.open(whatsAppUrl as URL, options: [:], completionHandler: nil)
            } else {

                UIApplication.shared.openURL(whatsAppUrl as URL)

            }
        }
        else{


            ProjectUtility.AlertWith(self, message: " What's App is Not Available.", Title: "Sorry")
        }

working fine But When I put This URL

("http://www.proprofs.com/quiz-school/story.php?title=pq-find-out-which-ice-age-character-you-are ")

Then Its Not Working Please Check Thanks.HelpWill Be Appriciated.




回答5:


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


来源:https://stackoverflow.com/questions/32042702/sending-message-to-whatsapp-from-your-app-using-swift

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