[英]How can I send message to specific contact through WhatsApp from my ios app using swift?
[英]Sending Whatsapp message to a specific contact number (Swift Project)
我正在嘗試將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>
兩個問題。
首先,這不是一個有效的網址方案。 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。
如果要發送到特定號碼,請使用以下代碼:
let whatsAppUrl = NSURL(string: "https://api.whatsapp.com/send?phone=**********&text=******")
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()
}
這個訣竅是有效的,因為你不直接調用whatsapp,而是瀏覽器,這個可以接收電話的數量,並打開whatsapp到所需的手機和所需的文字
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.