在iOS上打开whatsapp,预先填写新的电话号码

时间:2015-07-03 08:34:04

标签: ios objective-c whatsapp

我知道有与whatsapp沟通的方案,如:

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

但是我找不到如何使用之前不存在的电话号码打开whatsapp。

这是一个“contatc us”页面,如果我只能打开whatsapp而无法预先填写电话号码,那就没用了。我需要与whatsapp联系......

我想做的是什么?

7 个答案:

答案 0 :(得分:8)

预先填写的电话号码和文字

   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

答案 1 :(得分:1)

根据Whatsapp documentation,您需要在地址簿中实际联系,以便通过网址方案开启讨论。
那么对于你的问题

  

我尝试做的是否存在?

答案是:否。

enter image description here

答案 2 :(得分:1)

您可以在上面找到该whatsapp API的文档答案。根据我自己的试用添加更多的一点:

直接与特定联系人沟通,您需要该联系人。 ABID是保存联系人时系统自动生成的参数。因此,如果您的联系人中没有保存号码,那么您无法从应用程序中打开该号码。

我使用过一种解决方法。当您的应用程序首次加载到设备中时,会将联系我们号码保存到地址簿中。当数字保存成功后,您将在返回时获得ABID。您可以使用该ABID来调用该联系人的消息。

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

答案 3 :(得分:1)

如果有人在寻找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")
                }
            }
        }
    }

答案 4 :(得分:1)

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")
                }
            }
        }
    }

答案 5 :(得分:0)

  

对于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)
  }
}

答案 6 :(得分:0)

根据Whatsapp documentation

对于Swift 3.0或更高版本,这将打开数字的whatsapp聊天

数字示例:“ + 6599999999”

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