Ios 使用Swift从应用程序向WhatsApp发送消息?
对于我的一个应用程序,我想与WhatsApp联系人共享数据。我在StackOverflow上尝试了几个解决方案,但没有得到确切的解决方案。经过一些试验可以达到我所期待的,所以在这里分享给任何人的未来参考Ios 使用Swift从应用程序向WhatsApp发送消息?,ios,swift,whatsapp,Ios,Swift,Whatsapp,对于我的一个应用程序,我想与WhatsApp联系人共享数据。我在StackOverflow上尝试了几个解决方案,但没有得到确切的解决方案。经过一些试验可以达到我所期待的,所以在这里分享给任何人的未来参考 var url = NSURL(string: "whatsapp://send?text=Hello%20Friends%2C%20Sharing%20some%20data%20here...%20!") //Text which will be shared on WhatsApp
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编码。您可以使用internet上的任何开源工具或使用iOS中的addingPercentEncoding(withAllowedCharacters:)函数来获取它。
e、 g
我的代码是这样的
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")
}
工作正常,但当我把这个网址
("http://www.proprofs.com/quiz-school/story.php?title=pq-find-out-which-ice-age-character-you-are ")
那么它不工作了,请检查一下,谢谢。我们会通知您帮助。Swift 3.0
请尝试使用此代码在应用程序中访问watsapp。这对我来说非常有效
@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")
}
}
}
}
除了上述解决方案之外,从iOS 9开始,我们需要将whatsapp添加到info.plist中的LSApplicationQueriesSchemes密钥中。在这之后,只有它对我有效
Swift 5 代码
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")
}
}
}
Swift 5 请按照以下步骤通过URL方案在WhatsApp上共享
LSApplicationQueriesSchemes
WhatsApp
Hi@Pandurang亚克瓦德。我如何分享一个链接?你能给我演示一下吗。请。或者,当我试图共享www.Google.com时,我们是否可以共享一个Url长度,但我的Url太长,所以我的文本区域变为空白。@AvinashMishra我认为Url长度没有任何限制。只需检查该url是否直接在浏览器中工作。有时错误的url会导致问题。在浏览器中工作正常,不在此处。@AvinashMishra如果您共享代码,我可以查看并让您知道问题所在。是否有字符限制?是否有字符限制?您提到我必须发送消息的号码的地方???让urlString=”whatsapp://send?text=" + (self.modelForScreen?.deeplinkattransation???)如果让URL=URL(字符串:urlStringEncoded!),则让urlStringEncoded=urlString.addingPercentEncoding(允许字符:.UrlQueryLowed),UIApplication.shared.canOpenURL(URL为URL){UIApplication.shared.open(URL,选项:[:],completionHandler:nil)}else{Utility.showtoos(“Whatsapp未安装”)}
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")
}
}
}