20

For one of my app, I wanted to share data to WhatsApp contacts. I tried few solutions overs the StackOverflow but couldn't get exact solution. After some trials could achieve what I was looking for, so sharing here for anyone's future reference.

GameLoading
  • 6,688
  • 2
  • 33
  • 57
Pandurang Yachwad
  • 1,695
  • 1
  • 19
  • 29

7 Answers7

26
 var url  = NSURL(string: "whatsapp://send?text=Hello%20Friends%2C%20Sharing%20some%20data%20here...%20!")

//Text which will be shared on WhatsApp is: "Hello Friends, Sharing some data here... !"

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

Note: text needs to be URL encoded. You can get it using any of the open source tools over internet or using addingPercentEncoding(withAllowedCharacters:) function in iOS. e.g.

var urlString = "Hello Friends, Sharing some data here... !"
var urlStringEncoded = urlString.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
var url  = NSURL(string: "whatsapp://send?text=\(urlStringEncoded!)")
Abhishek
  • 1,261
  • 1
  • 13
  • 30
Pandurang Yachwad
  • 1,695
  • 1
  • 19
  • 29
8

Swift 5

Add whatsapp in LSApplicationQuerySchemes(Info.plist)

Code

let urlWhats = "whatsapp://send?text=\("Hello World")"
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) {
      if let whatsappURL = NSURL(string: urlString) {
            if UIApplication.shared.canOpenURL(whatsappURL as URL) {
                 UIApplication.shared.open(whatsappURL as URL)
             } 
             else {
                 print("please install watsapp")
             }
      }
}
Barath
  • 1,656
  • 19
  • 19
7

Swift 3.0

Try with this code for access watsapp in your application. Its working perfectly for me.

@IBAction func sendButtonAction(_ sender: Any)
{
    let date = Date()
    let msg = "Hi my dear friends\(date)"
    let urlWhats = "whatsapp://send?text=\(msg)"

    if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) {
        if let whatsappURL = NSURL(string: urlString) {
            if UIApplication.shared.canOpenURL(whatsappURL as URL) {
                UIApplication.shared.openURL(whatsappURL as URL)
            } else {
                print("please install watsapp")
            }
        }
    }
}
6

Addition to above solutions, starting from iOS 9, we need to add whatsapp to LSApplicationQueriesSchemes key in info.plist. After this only it worked for me.

screen shot of solution

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Lokesh Purohit
  • 523
  • 6
  • 9
2

My code is Looking Like this

let encodeQuizStr = "Check Out The Quiz With link \n http://www.proprofs.com "

        let urlQuizStringEncoded = encodeQuizStr.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)

        guard let whatsAppUrl = NSURL(string: "whatsapp://send?text="+urlQuizStringEncoded!) else { return }

        if UIApplication.shared.canOpenURL(whatsAppUrl as URL) {

            if #available(iOS 10.0, *) {
                print(urlQuizStringEncoded!)
                UIApplication.shared.open(whatsAppUrl as URL, options: [:], completionHandler: nil)
            } else {

                UIApplication.shared.openURL(whatsAppUrl as URL)

            }
        }
        else{


            ProjectUtility.AlertWith(self, message: " What's App is Not Available.", Title: "Sorry")
        }

working fine But When I put This URL

("http://www.proprofs.com/quiz-school/story.php?title=pq-find-out-which-ice-age-character-you-are ")

Then Its Not Working Please Check Thanks.HelpWill Be Appriciated.

Avinash Mishra
  • 797
  • 9
  • 19
  • let urlString = "whatsapp://send?text=" + (self.modelForScreen?.deeplinkNarration ?? " ") let urlStringEncoded = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) if let URL = URL(string: urlStringEncoded!), UIApplication.shared.canOpenURL(URL as URL) { UIApplication.shared.open(URL, options: [:], completionHandler: nil) }else{ Utility.showToast("Whatsapp not installed.") } – Umesh Sharma Oct 10 '19 at 05:38
2

Swift 5

Please follow the below steps for sharing on WhatsApp through URL Schemes

  1. Add this code in your app "info.plist"

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

enter image description here

  1. For sharing text and URL

Code

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

Happy Coding!

Nitin
  • 1,383
  • 10
  • 19
0

As per their FAQ, you should be using the universal links instead:

https://wa.me/1234567890

Reference: https://faq.whatsapp.com/563219570998715/?locale=en_US

ekscrypto
  • 3,718
  • 1
  • 24
  • 38