在iOS上打开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 …

我想做的是否存在?

对于预先填写的电话号码和文本

  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文件中添加此行

 LSApplicationQueriesSchemes  whatsapp://  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) } }