如何从 iOS 应用程序向 Whatsapp 中的联系人发送消息 - Swift
How to send message to contact in Whatsapp from iOS App - Swift
我正在尝试从 Swift 开发的 iOS 应用程序向 Whatsapp 联系人发送消息。但是特定的联系人没有打开,而是打开了我在 whatsApp 中的所有联系人。
到目前为止我的代码 -
func messageViaWhatsApp (sender: AnyObject) {
let messageBody = "Hello"
let whatsURL = "whatsapp://send?text=\(messageBody)"
let whatsAppURL = NSURL(string: whatsURL.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!)
if UIApplication.sharedApplication().canOpenURL(whatsAppURL!)
{
UIApplication.sharedApplication().openURL(whatsAppURL!)
}
else
{
let alert = UIAlertView(title: "Sorry", message: "Your device does not have whatsApp installed ", delegate: nil, cancelButtonTitle: "OK")
}
}
感谢您的帮助。 XCode - 8.0,Swift 2.3
出于安全考虑,Apple 不允许您发送给特定联系人。
试试这个....
let urlWhats = "whatsapp://send?phone=***********&text=***"
var characterSet = CharacterSet.urlQueryAllowed
characterSet.insert(charactersIn: "?&")
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: characterSet){
if let whatsappURL = NSURL(string: urlString) {
if UIApplication.shared.canOpenURL(whatsappURL as URL){
UIApplication.shared.openURL(whatsappURL as URL)
}
else {
print("Install Whatsapp")
}
}
}
Note:Country code (Ex:+91) is mandatory to open mobile number Chat
注意:在info.plist
中添加url方案
<key>LSApplicationQueriesSchemes</key>
<array>
<string>whatsapp</string>
</array>
我正在尝试从 Swift 开发的 iOS 应用程序向 Whatsapp 联系人发送消息。但是特定的联系人没有打开,而是打开了我在 whatsApp 中的所有联系人。
到目前为止我的代码 -
func messageViaWhatsApp (sender: AnyObject) {
let messageBody = "Hello"
let whatsURL = "whatsapp://send?text=\(messageBody)"
let whatsAppURL = NSURL(string: whatsURL.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!)
if UIApplication.sharedApplication().canOpenURL(whatsAppURL!)
{
UIApplication.sharedApplication().openURL(whatsAppURL!)
}
else
{
let alert = UIAlertView(title: "Sorry", message: "Your device does not have whatsApp installed ", delegate: nil, cancelButtonTitle: "OK")
}
}
感谢您的帮助。 XCode - 8.0,Swift 2.3
出于安全考虑,Apple 不允许您发送给特定联系人。
试试这个....
let urlWhats = "whatsapp://send?phone=***********&text=***"
var characterSet = CharacterSet.urlQueryAllowed
characterSet.insert(charactersIn: "?&")
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: characterSet){
if let whatsappURL = NSURL(string: urlString) {
if UIApplication.shared.canOpenURL(whatsappURL as URL){
UIApplication.shared.openURL(whatsappURL as URL)
}
else {
print("Install Whatsapp")
}
}
}
Note:Country code (Ex:+91) is mandatory to open mobile number Chat
注意:在info.plist
中添加url方案<key>LSApplicationQueriesSchemes</key>
<array>
<string>whatsapp</string>
</array>