【问题标题】:Sending Whatsapp message to a specific contact number (Swift Project)将 Whatsapp 消息发送到特定的联系号码(Swift 项目)
【发布时间】:2016-09-25 12:48:39
【问题描述】:

我正在尝试向存储在全局变量中的收件人号码发送 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发短信?

【问题讨论】:

    标签: swift url social-networking whatsapp


    【解决方案1】:

    试试这个....

     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 scheme

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

    【讨论】:

    • 谢谢,我可以直接向某个号码发送短信
    • 是否有可能因为直接发送给用户而导致应用被拒绝
    • @SangramShivankar 没有,应用商店审核没有问题。
    • 嗨,我可以将消息发送到多个电话号码
    • 这不起作用,whatsapp 打开并显示错误“无法打开链接检查链接并重试。”
    【解决方案2】:

    两个问题。

    首先是这不是一个有效的 url 方案。 URL 方案采用identifier://params 格式,因此您需要改用whatsapp://phone_number

    其次,Apple 现在要求您在 Info.plist 文件中定义应用程序使用的外部 url 方案,嵌套在键 LSApplicationQueriesSchemes 下。请参阅iOS 9 not opening Instagram app with URL SCHEME 了解更多信息。


    根据 Whatsapp URL 方案文档,您实际上无法提供要将消息发送到的联系人的电话号码:https://www.whatsapp.com/faq/en/iphone/23559013

    但是,您可以提供要发送给他们的消息:

    whatsapp://send?text=Some%20Text.

    确保文本是百分比编码的,否则 NSURL 将无法从提供的字符串创建有效的 URL。

    【讨论】:

    • 我做到了!但是我只能打开whatsapp应用程序,而不是向我想要发送消息的收件人?
    • 如何将其直接发送给收件人?
    • @Mariah 根据他们的文档:whatsapp.com/faq/en/iphone/23559013,这似乎是不可能的,但是您可以指定文本。请查看更新后的答案。
    • @max_ 谢谢。
    • 有字数限制吗?
    【解决方案3】:

    如果您想发送到特定号码,请使用以下代码:

      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到所需的电话和所需的文本

    【讨论】:

    • 我可以向多个号码发送一条消息
    • @shubhammishra 到目前为止,我现在是不可能的,但是您可以通过发送相同的消息来简单地迭代 foreach 电话号码
    • 谢谢兄弟,但我必须处理第一个成功的下一次迭代调用,因为在 for 循环中调用 openURL() 将导致多次打开 WhatsApp,最终会打开最后一个号码的聊天屏幕。所以,基本上我可以在 openURL() 之后有闭包或任何类型的回调。如果您找到解决方案,请将其添加到您的答案中。
    • 这不起作用,whatsapp 打开并显示错误“无法打开链接检查链接并重试。”
    猜你喜欢
    • 1970-01-01
    • 2013-09-24
    • 2013-09-04
    • 2015-07-08
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多