【问题标题】:How to send message and link to WhatsApp in Swift 3?如何在 Swift 3 中发送消息和链接到 WhatsApp?
【发布时间】:2017-12-20 01:54:13
【问题描述】:

如何在 Swift 3 中发送消息和链接到 WhatsApp

我正在使用此代码: Code

向控制台发送错误消息:

... URL 失败:“whatsapp://send?text=Check” - 错误:“此应用是 不允许查询方案whatsapp”

谢谢

【问题讨论】:

标签: swift3 whatsapp


【解决方案1】:

斯威夫特 5

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

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

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

  1. 用于分享文字和网址

     @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 char 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】:

    你应该试试这个:

    注意:您的设备中必须安装 whatsapp 应用程序。

    斯威夫特 3

    var documentInteractionController: UIDocumentInteractionController = UIDocumentInteractionController()
    
    @IBAction func whatsappShareText(_ sender: AnyObject)
    {
        let originalString = "First Whatsapp Share"
        let escapedString = originalString.addingPercentEncoding(withAllowedCharacters:CharacterSet.urlQueryAllowed)
    
        let url  = URL(string: "whatsapp://send?text=\(escapedString!)")
    
        if UIApplication.shared.canOpenURL(url! as URL)
        {
            UIApplication.shared.open(url! as URL, options: [:], completionHandler: nil)
        }
    }
    
    @IBAction func whatsappShareLink(_ sender: AnyObject)
    {
        let originalString = "https://www.google.co.in"
        let escapedString = originalString.addingPercentEncoding(withAllowedCharacters:CharacterSet.urlQueryAllowed)
        let url  = URL(string: "whatsapp://send?text=\(escapedString!)")
    
        if UIApplication.shared.canOpenURL(url! as URL)
        {
            UIApplication.shared.open(url! as URL, options: [:], completionHandler: nil)
        }
    }
    

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

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

    【讨论】:

      猜你喜欢
      • 2019-09-19
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      • 1970-01-01
      • 2022-09-30
      相关资源
      最近更新 更多