【问题标题】:Open Whatsapp on a particular number in swift快速在特定号码上打开 Whatsapp
【发布时间】:2017-03-24 22:28:04
【问题描述】:

我正在尝试在 whatsapp 中打开特定的联系人聊天,但没有得到任何解决方案。请帮助我完全陷入困境。我试过这个:

let whatsAppURL: NSURL = NSURL(string: "whatsapp://send?abid=\(primary)&;text=lOL;")!
        if UIApplication.sharedApplication().canOpenURL(whatsAppURL){
            UIApplication.sharedApplication().openURL(whatsAppURL)
        }

【问题讨论】:

  • 如果你卡住了,你应该有代码在这里显示。
  • 实际上@Sheereen S 找到了正确的工作答案。我建议 OP 将他的答案设置为最佳。 stackoverflow.com/a/45351187/2150954

标签: ios whatsapp


【解决方案1】:

您可以向特定用户发送消息。

直接应用聊天网址打开

let urlWhats = "whatsapp://send?phone=+919789384445&abid=12354&text=Hello"
    if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) {
        if let whatsappURL = URL(string: urlString) {
            if UIApplication.shared.canOpenURL(whatsappURL!) {
                UIApplication.shared.openURL(whatsappURL!)
            } else {
                print("Install Whatsapp")
            }
        }
    }

注意:国家代码(例如:+91)是打开手机号码聊天的必填项

WebUrl 链接打开聊天

 let whatsappURL = URL(string: "https://api.whatsapp.com/send?phone=9512347895&text=Invitation")
    if UIApplication.shared.canOpenURL(whatsappURL) {
        UIApplication.shared.open(whatsappURL, options: [:], completionHandler: nil)
    }

查看以下链接,

https://www.whatsapp.com/faq/en/general/26000030

注意:在 info.plist 中添加 url scheme

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

【讨论】:

  • 如果这个答案不起作用,请尝试不加+,它应该可以工作,我只是尝试过它可以工作
【解决方案2】:

适用于 Swift 4.2 / Swift 5

func openWhatsapp(){
    let urlWhats = "whatsapp://send?phone=(mobile number with country code)"
    if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: CharacterSet.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 {
                    UIApplication.shared.openURL(whatsappURL)
                }
            }
            else {
                print("Install Whatsapp")
            }
        }
    }
}

对于 Swift 2.0

let urlWhats = "whatsapp://send?phone=(mobile number with country code)"
        if let urlString = urlWhats.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()){
            if let whatsappURL = NSURL(string: urlString) {
                if UIApplication.sharedApplication().canOpenURL(whatsappURL){
                    UIApplication.sharedApplication().openURL(whatsappURL)
                }
                else {
                    print("Install Whatsapp")
                }
            }
        }

注意:在 info.plist 中添加 url scheme

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

【讨论】:

  • 此解决方案有效,但我想知道为什么添加 url 方案,因为该解决方案在 info.plist 中没有 url 方案仍然有效
【解决方案3】:

这对我很有效

    if let url = URL(string: "https://wa.me/91xxxxxxxxxx?text=Hello"),
            UIApplication.shared.canOpenURL(url) {
                UIApplication.shared.open(url, options: [:])
    }

【讨论】:

    【解决方案4】:
    1. Info.plist 源代码文件添加属性,如下所示:

      <key>LSApplicationQueriesSchemes</key>
      <array>
          <string>whatsapp</string>
      </array>
      
    2. 在您的应用中自定义点击时使用以下功能:

      func openWhatsapp(){
          let urlWhats = "https://wa.me/numberWithCountryCode"
          if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: CharacterSet.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 {
                          UIApplication.shared.openURL(whatsappURL)
                      }
                  } else {
                      Alert(withMessage: "You do not have WhatsApp installed! \nPlease install first.").show(andCloseAfter: 2.0)
                  }
              }
          }
      }
      

    【讨论】:

      【解决方案5】:

      根据whatsapp forum link,您无法向特定用户发送消息,这在 whatsapp URL 方案中不可用。

      您只需设置预定义的消息,然后使用 URL 方案,您就可以打开 whatsapp 最近的控制器。

      【讨论】:

      【解决方案6】:

      这个问题真的很有帮助

      只需输入不带“+”的人员编号 示例:60161234567

      【讨论】:

        【解决方案7】:

        这是不可能的,你可以用 URL 方案打开 WhatsApp。

        【讨论】:

        • 我们可以使用上面的代码打开 Whatsapp 联系人聊天。
        猜你喜欢
        • 1970-01-01
        • 2015-01-31
        • 1970-01-01
        • 2013-09-04
        • 2015-09-21
        • 1970-01-01
        • 2015-07-08
        • 1970-01-01
        • 2020-01-04
        相关资源
        最近更新 更多