1

I have my app running on my iPhone, and I'm able to send some data to my WhatsApp from my App. I went through the following to do the same :

Sending message to WhatsApp from your app using Swift?

Is there a way we can create a URL that would open our own app directly if it is installed? Could someone suggest any place where I could understand how it could be done?

Community
  • 1
  • 1
221b
  • 431
  • 1
  • 11
  • 34

1 Answers1

1

Something like this should work:

let originalString = "Some text you want to send"
let encodedString = originalString.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
let url = NSURL(string: "whatsapp://send?text="+encodedString!)

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

} else {

    print("whatsapp not installed")
}
Abhishek
  • 1,261
  • 1
  • 13
  • 30
Kex
  • 8,023
  • 9
  • 56
  • 129