簡體   English   中英

使用預先填寫的新電話號碼在 iOS 上打開 whatsapp

[英]Open whatsapp on iOS with new phone number pre-filled

我知道有與 whatsapp 通信的計划,例如:

NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=Hello%2C%20World!"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
    [[UIApplication sharedApplication] openURL: whatsappURL];
}

但是我找不到如何使用以前不存在的電話號碼打開 whatsapp。

這是一個“聯系我們”頁面,如果我只能打開whatsapp而不能預先填寫電話號碼,那就沒用了。 我需要與whatsapp聯系...

我想要做的事情存在嗎?

對於預填電話號碼和文本

   NSURL *whatsappURL = [NSURL URLWithString:@"https://api.whatsapp.com/send?phone=9530670491&text=hello"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
    [[UIApplication sharedApplication] openURL: whatsappURL];
}

單擊此處獲取Swift

檢查鏈接了解更多詳情https://www.whatsapp.com/faq/en/general/26000030

根據Whatsapp 文檔,您需要在地址簿中實際擁有聯系人才能從 url 方案打開討論。
所以對於你的問題

我嘗試做的事情存在嗎?

答案是:不。

在此處輸入圖片說明

您可以在答案上方找到該 whatsapp API 的文檔。 根據我自己的試驗,在這方面增加了一些要點:

要與特定聯系人直接溝通,您需要與該聯系人聯系。 ABID 是保存聯系人時系統自動生成的參數。 因此,如果您的聯系人中沒有保存號碼,則無法從應用程序中打開該號碼。

我使用了一種解決方法。 當您的應用程序首次加載到設備中時,您將聯系我們的號碼保存到地址簿中。 成功保存號碼后,您將在返回中獲得 ABID。 您可以使用該 ABID 調用與該聯系人的消息傳遞。

NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?abid=123&text=Hello%2C%20World!"];

如果有人在尋找 Swift 版本

static func triggerWhats(_ vc:UIViewController){
        let msg = "Hello! I have a question"
        let urlWhats = "whatsapp://send?phone=+6599999999&text=\(msg)"
        if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed){
            if let whatsappURL = NSURL(string: urlString) {
                if UIApplication.shared.canOpenURL(whatsappURL as URL) {
                    _ = UIApplication.shared.open(whatsappURL as URL, options: [:], completionHandler: nil)
                } else {
                    // Cannot open whatsapp
                    ViewUtil().popAlertView(vc, title: "WhatsApp Support", message: "Please WhatsApp us at +65999999 if you have any questions", option: "Ok")
                }
            }
        }
    }

Swift 3.0 打開什么應用程序。

info.plist文件中添加這一行

<key>LSApplicationQueriesSchemes</key>
<array>
       <string>whatsapp://</string>
</array>

func OpenWhatsApp(_ vc:UIViewController){
        let msg = "" // here pass your message 
        let urlWhats = "https://api.whatsapp.com/send?phone=(Mobileno)&text=\(msg)" // Mobile number that include country code and without + sign . 
        if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed){
            if let whatsappURL = URL(string: urlString) {
                if UIApplication.shared.canOpenURL(whatsappURL) {
                    if #available(iOS 10.0, *) {
                        _ = UIApplication.shared.open(whatsappURL, options: [:], completionHandler: nil)
                    } else {
                        // Fallback on earlier versions
                        UIApplication.shared.canOpenURL(whatsappURL)
                    }
                } else {
                    // Cannot open whatsapp
                    AppUtilities.sharedInstance.showAlert(title: "WhatsApp Support", msg: "Your device is not support whats app")
                }
            }
        }
    }

對於 Swift 3.0 或更高版本,這將打開一個數字的 whatsapp 聊天

數字示例:“+918798766554”

if let whatappURL = URL(string: "https://api.whatsapp.com/send?phone=\(number)&text=\(msg)"),
  UIApplication.shared.canOpenURL(whatappURL)
{
  if #available(iOS 10.0, *) {
    UIApplication.shared.open(whatappURL, options: [:], completionHandler: nil)
  } else {
    UIApplication.shared.openURL(whatappURL)
  }
}

根據Whatsapp 文檔

對於 Swift 3.0 或更高版本,這將打開一個數字的 whatsapp 聊天

數字示例:“+6599999999”

UIApplication.shared.open(URL(string: "https://wa.me/+6599999999")!)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM