Mar*_*iah 4 url social-networking whatsapp swift
我正在尝试将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()
}
Run Code Online (Sandbox Code Playgroud)
我总是得到警告信息,这是其他情况!虽然数字总是如此!可能是url语法中的错误?
在控制台中:
canOpenURL: failed for URL: "whatsapp:0534260282" -
"This app is not allowed to query for scheme whatsapp"
Run Code Online (Sandbox Code Playgroud)
这是正确的方法吗?或者这种方式只是为了分享,通过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")
}
}
}
Run Code Online (Sandbox Code Playgroud)
注意:国家代码(例如:+91)是打开手机号码聊天所必需的
注意:在info.plist中添加url方案
<key>LSApplicationQueriesSchemes</key>
<array>
<string>whatsapp</string>
</array>
Run Code Online (Sandbox Code Playgroud)
两个问题。
第一个是这不是有效的 url 方案。URL 方案采用该格式,identifier://params
因此您需要改用该格式whatsapp://phone_number
。
其次,Apple 现在要求您在 Info.plist 文件中定义应用程序使用的外部 url 方案,该文件嵌套在键 LSApplicationQueriesSchemes 下。有关详细信息,请参阅iOS 9 未使用 URL SCHEME 打开 Instagram 应用程序。
根据 Whatsapp URL 方案文档,您实际上无法提供要将消息发送到的联系人的电话号码:https : //www.whatsapp.com/faq/en/iphone/23559013。
但是,您可以提供要发送给他们的消息:
whatsapp://send?text=Some%20Text
.
确保文本是百分比编码的,否则 NSURL 将无法从提供的字符串创建有效的 URL。
归档时间: |
|
查看次数: |
8575 次 |
最近记录: |