对于我的一个应用程序,我想分享数据到WhatsApp联系人。我在StackOverflow上尝试了一些解决方案,但无法获得确切的解决方案。经过一些试验可以达到我所期望的,所以在这里分享给任何人的未来参考。使用Swift从您的应用程序发送消息给WhatsApp?
11
A
回答
16
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().openURL(url!)
}
注意:文本需要进行网址编码。您可以通过互联网上的任何开源工具或使用iOS中的stringByAddingPercentEncodingWithAllowedCharacters函数获取它。 例如
var urlString = "Hello Friends, Sharing some data here... !"
var urlStringEncoded = urlString.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())
var url = NSURL(string: "whatsapp://send?text=\(urlStringEncoded!)")
1
我的代码看起来像这样
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 ")
然后它不工作,请检查Thanks.HelpWill是Appriciated。
5
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")
}
}
}
}
+0
是否有任何字符限制? –
相关问题
- 1. 从应用程序发送一个WhatsApp消息
- 2. 消息应用程序/ Whatsapp发送按钮图像
- 3. 使用php发送Whatsapp消息
- 4. 通过whatsapp使用Intent发送消息
- 5. 从iPhone应用程序发送消息
- 6. 发送一条消息从应用程序to watchOS在Swift
- 7. 我可以在我的应用程序中使用whatsapp发送消息
- 8. 从facebook应用程序发送消息给用户
- 9. 发送消息给Twitter的追随者从Facebook应用程序
- 10. 在不打开Whatsapp的情况下从WhatsApp发送消息给Whatsapp
- 11. 从外部发送消息给C++/Qt应用程序?
- 12. 从应用程序代理发送消息给viewController
- 13. 从javascript发送消息给.NET应用程序?
- 14. SignalR从外部应用程序发送消息给客户端
- 15. 发送android应用程序的消息?
- 16. 使用MQTT从Web应用程序发送消息给我的Raspberry PI
- 17. 通过应用程序发送消息
- 18. 发送消息给其他用户Android应用程序
- 19. Facebook应用程序:发送消息给用户
- 20. 如何使用C#编程发送消息到whatsapp数字
- 21. 发送信息给WPF应用程序
- 22. WP8 Facebook发送消息应用程序到应用程序
- 23. 如何将应用程序发送至应用程序消息
- 24. 如何发送消息并链接到Swift 3中的WhatsApp?
- 25. 发送通知消息给vb.net应用程序
- 26. Android - 发送App消息给其他Messenger应用程序
- 27. 从移动设备发送SMS,MMS消息并将该消息发送给Java应用程序服务器
- 28. 从Java应用程序发送消息到侦听的Android应用程序
- 29. 从Phonegap应用程序中提取WhatsApp消息
- 30. 发送与根的Whatsapp消息
Hi @ Pandurang Yachwad。我如何分享一个链接,你可以向我演示一个演示。 please.or是否有一个长度的URL我们可以分享,因为我试图分享www.Google.com哪些工作,但我的网址太长,所以我在文本区域变得空白。 –
@AvinashMishra我不认为URL长度有任何限制。只需检查该网址是否可以直接在浏览器中运行。有时坏的网址会导致问题。 –
在浏览器中正常工作不在此处。 –