我正在尝试向存储在全局变量中的收件人号码发送 Whatsapp 消息!
通过使用这个简单的代码:
let whatsAppUrl = NSURL(string: "whatsapp:\(globalPhone)")
if UIApplication.shared.canOpenURL(whatsAppUrl as! URL) {
UIApplication.shared.openURL(whatsAppUrl as! URL)
} else {
let errorAlert = UIAlertView(
title: "Sorry",
message: "You can't send a message to this number",
delegate: self,
cancelButtonTitle:"Ok"
)
errorAlert.show()
}
我总是收到警报消息,这是其他情况!
虽然数字总是真实的!
可能是 url 语法错误?
在控制台中:
canOpenURL: failed for URL: "whatsapp:0534260282" -
"This app is not allowed to query for scheme whatsapp"
这是正确的方法吗?
或者这种方式只是为了分享,通过 Whatsapp 发短信?
尝试这个....
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")
}
}
注意:打开手机号码聊天必须输入国家代码(例如:+91)
注意:在info.plist中添加url方案
<key>LSApplicationQueriesSchemes</key>
<array>
<string>whatsapp</string>
</array>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)