【问题标题】:Sending message to WhatsApp from your app using Swift?使用 Swift 从您的应用程序向 WhatsApp 发送消息?
【发布时间】:2015-11-09 15:16:28
【问题描述】:

对于我的一款应用,我想将数据分享给 WhatsApp 联系人。我在 StackOverflow 上尝试了一些解决方案,但无法获得确切的解决方案。经过一些试验可以实现我想要的,所以在这里分享以供任何人将来参考。

【问题讨论】:

    标签: ios swift whatsapp


    【解决方案1】:

    斯威夫特 5

    请按照以下步骤通过 URL Schemes 在 WhatsApp 上分享

    1. 将此代码添加到您的应用“info.plist”中

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

    1. 用于共享文本和 URL

    代码

        @IBAction func whatsappShareText(_ sender: AnyObject) {
    
        
            let message = "First Whatsapp Share & https://www.google.co.in"
            var queryCharSet = NSCharacterSet.urlQueryAllowed
            
            // if your text message contains special characters like **+ and &** then add this line
            queryCharSet.remove(charactersIn: "+&")
            
            if let escapedString = message.addingPercentEncoding(withAllowedCharacters: queryCharSet) {
                if let whatsappURL = URL(string: "whatsapp://send?text=\(escapedString)") {
                    if UIApplication.shared.canOpenURL(whatsappURL) {
                        UIApplication.shared.open(whatsappURL, options: [: ], completionHandler: nil)
                    } else {
                        debugPrint("please install WhatsApp")
                    }
                }
            }
        }
    

    编码愉快!

    【讨论】:

      【解决方案2】:

      斯威夫特 5

      代码

      let urlWhats = "whatsapp://send?text=\("Hello World")"
      if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) {
            if let whatsappURL = NSURL(string: urlString) {
                  if UIApplication.shared.canOpenURL(whatsappURL as URL) {
                       UIApplication.shared.open(whatsappURL as URL)
                   } 
                   else {
                       print("please install watsapp")
                   }
            }
      }
      

      【讨论】:

        【解决方案3】:

        除了上述解决方案,从iOS 9开始,我们需要在info.plist中的LSApplicationQueriesSchemes key中添加whatsapp。在此之后,它只对我有用。

        【讨论】:

          【解决方案4】:
           var url  = NSURL(string: "whatsapp://send?text=Hello%20Friends%2C%20Sharing%20some%20data%20here...%20!")
          
          //Text which will be shared on WhatsApp is: "Hello Friends, Sharing some data here... !"
          
              if UIApplication.sharedApplication().canOpenURL(url!) {
                  UIApplication.sharedApplication().open(url as URL, options: [:]) { (success) in
                          if success {
                              print("WhatsApp accessed successfully")
                          } else {
                              print("Error accessing WhatsApp")
                          }
                      }
              }
          

          注意:文本需要进行 URL 编码。您可以通过 Internet 使用任何开源工具或在 iOS 中使用 addPercentEncoding(withAllowedCharacters:) 函数来获取它。 例如

          var urlString = "Hello Friends, Sharing some data here... !"
          var urlStringEncoded = urlString.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
          var url  = NSURL(string: "whatsapp://send?text=\(urlStringEncoded!)")
          

          【讨论】:

          • 嗨@Pandurang Yachwad。我如何分享链接,你能给我演示一下吗?请。或者当我尝试分享 www.Google.com 时,是否有一段我们可以分享的网址,但我的网址太长了,所以我在文本区域中变得空白。
          • @AvinashMishra 我认为 URL 长度没有任何限制。只需检查该网址是否直接在浏览器中有效。有时错误的网址会导致问题。
          • 在浏览器中无法正常工作。
          • @AvinashMishra 如果你分享你的代码,我可以看看,让你知道有什么问题
          • 有字数限制吗?
          【解决方案5】:

          斯威夫特 3.0

          尝试使用此代码在您的应用程序中访问 watsapp。它非常适合我。

          @IBAction func sendButtonAction(_ sender: Any)
          {
              let date = Date()
              let msg = "Hi my dear friends\(date)"
              let urlWhats = "whatsapp://send?text=\(msg)"
          
              if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) {
                  if let whatsappURL = NSURL(string: urlString) {
                      if UIApplication.shared.canOpenURL(whatsappURL as URL) {
                          UIApplication.shared.openURL(whatsappURL as URL)
                      } else {
                          print("please install watsapp")
                      }
                  }
              }
          }
          

          【讨论】:

          • 有字数限制吗?
          • 你在哪里提到了我必须发送消息的号码????
          【解决方案6】:

          我的代码看起来像这样

          let encodeQuizStr = "Check Out The Quiz With link \n http://www.proprofs.com "
          
                  let urlQuizStringEncoded = encodeQuizStr.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
          
                  guard let whatsAppUrl = NSURL(string: "whatsapp://send?text="+urlQuizStringEncoded!) else { return }
          
                  if UIApplication.shared.canOpenURL(whatsAppUrl as URL) {
          
                      if #available(iOS 10.0, *) {
                          print(urlQuizStringEncoded!)
                          UIApplication.shared.open(whatsAppUrl as URL, options: [:], completionHandler: nil)
                      } else {
          
                          UIApplication.shared.openURL(whatsAppUrl as URL)
          
                      }
                  }
                  else{
          
          
                      ProjectUtility.AlertWith(self, message: " What's App is Not Available.", Title: "Sorry")
                  }
          

          工作正常但是当我输入这个 URL 时

          ("http://www.proprofs.com/quiz-school/story.php?title=pq-find-out-which-ice-age-character-you-are ")
          

          然后它不工作请检查Thanks.HelpWill Weappriciated。

          【讨论】:

          • let urlString = "whatsapp://send?text=" + (self.modelForScreen?.deeplinkNarration ?? " ") let urlStringEncoded = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) if let URL = URL(string: urlStringEncoded!), UIApplication.shared.canOpenURL(URL as URL) { UIApplication.shared.open(URL, options: [:], completionHandler: nil) }else{ Utility.showToast("Whatsapp 未安装。" ) }
          猜你喜欢
          • 2015-11-25
          • 1970-01-01
          • 2017-04-08
          • 2014-09-07
          • 2019-08-03
          • 2018-07-11
          • 1970-01-01
          • 2013-10-08
          • 2019-09-17
          相关资源
          最近更新 更多