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

时间:2017-07-14 21:30:20

标签: swift3 whatsapp

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

我正在使用此代码:   Code

消息错误到控制台:

  

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

谢谢

2 个答案:

答案 0 :(得分:12)

你应该试试这个:

注意:您必须在设备中安装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>

答案 1 :(得分:0)

快捷键5

请按照以下步骤通过URL方案在WhatsApp上共享

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

readonly'  => 'readonly'

enter image description here

  1. 用于共享文本和URL

    [   // Text
        'name'  => 'id',
        'label' => "ID",
        'type'  => 'text',
    'readonly'  => 'readonly',
    ],
    

    }

快乐编码!