首页 文章

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

提问于
浏览
4

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

我正在使用此代码:Code

消息错误到控制台:

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

谢谢

1 回答

  • 11

    你应该试试这个:

    Note: 您必须在设备中安装whatsapp app .

    Swift 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>
    

相关问题