使用Swift从您的应用程序向WhatsApp发送消息? [英] Sending message to WhatsApp from your app using Swift?
本文介绍了使用Swift从您的应用程序向WhatsApp发送消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于我的某个应用,我想与WhatsApp联系人分享数据。我在StackOverflow上尝试了一些解决方案,但无法获得精确的解决方案。经过一些试验可以达到我想要的目的,所以在这里分享给任何人未来的参考。
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.
推荐答案
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")
}
}
}
注意:文本需要进行URL编码。您可以使用互联网上的任何开源工具或在iOS中使用addsPercentEncoding(withAllowedCharacters :)功能来获取它。
例如。
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!)")
这篇关于使用Swift从您的应用程序向WhatsApp发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文