Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send message and link to WhatsApp in Swift 3?

How to send a message and link to WhatsApp in Swift 3

I'm using this code: Code

Message errore to the console:

...failed for URL: "whatsapp://send?text=Check" - error: "This app is not allowed to query for scheme whatsapp"

Thank you

like image 683
GURU Avatar asked Jul 14 '17 21:07

GURU


1 Answers

You should try this:

Note: You must have whatsapp app installed into your device.

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)
    }
}

Add this code in your app "info.plist"

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>whatsapp</string>
</array>
like image 89
Pragnesh Vitthani Avatar answered Nov 29 '22 07:11

Pragnesh Vitthani