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.
7 Answers
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!)")

- 1,261
- 1
- 13
- 30

- 1,695
- 1
- 19
- 29
-
Hi@Pandurang Yachwad. How Do i share a link can you SHOW me a Demo. please.or is there a lenght of Url Which We can Share as I tried to Share www.Google.com Which Works But my URL is Too Long So I am Getting Blank In Text Area. – Avinash Mishra Oct 12 '16 at 09:57
-
@AvinashMishra I don't think any limit for URL length. just check if that url works just directly in browser. sometime bad url causes the issue. – Pandurang Yachwad Oct 12 '16 at 17:19
-
Working Fine in Browser not Here. – Avinash Mishra Oct 13 '16 at 04:07
-
@AvinashMishra if you share you code, I can have look and let you know what's the issue – Pandurang Yachwad Oct 13 '16 at 14:13
-
I have Post my Code in Answer please Check and Sorry For New Question as i am not able to post New Question. Thats why i did This Sorry. – Avinash Mishra Oct 14 '16 at 12:37
-
@AvinashMishra Sorry, couldn't respond earlier as I was traveling. You need to encode the URL, please use encoded url as below for your url: – Pandurang Yachwad Oct 19 '16 at 16:11
-
http%3A%2F%2Fwww.proprofs.com%2Fquiz-school%2Fstory.php%3Ftitle%3Dpq-find-out-which-ice-age-character-you-are – Pandurang Yachwad Oct 19 '16 at 16:12
-
not able to find the specific Line What would Be the Line Can You Please Sahre in SWIFT 3.Thanks – Avinash Mishra Oct 20 '16 at 05:18
-
http%3A%2F%2Fwww.proprofs.com%2Fquiz-school%2Fstory.php%3Ftitle%3Dpq-find-out-which-ice-age-character-you-are – Pandurang Yachwad Oct 20 '16 at 10:38
-
i mean to say which line of code will give me this kind of result. – Avinash Mishra Oct 20 '16 at 11:08
-
I used http://meyerweb.com/eric/tools/dencoder/ for encoding the URL. This is good for hard coded URL. if you need dynamic URL, you can use programatic solution as in this StackOverflow Q&A. http://stackoverflow.com/questions/24879659/how-to-encode-a-url-in-swift – Pandurang Yachwad Oct 20 '16 at 17:51
-
@PandurangYachwad Can you please tell me how to open whatsapp from iOS app with a particular number. As you've shown here, it will open Whatsapp with a msg but I want to open Chat section of whatsapp with a particular number. – AbaEesa Mar 29 '17 at 13:57
-
1Is there any character limit? – Vaibhav Jhaveri Oct 04 '17 at 08:42
-
@VaibhavJhaveri I don’t think there is any limit for number of characters as far as they are in right format – Pandurang Yachwad Oct 04 '17 at 12:54
-
@PandurangYachwad my message gets trimmed off from the between – Vaibhav Jhaveri Oct 05 '17 at 04:28
-
For trimming issue is there any fixes @VaibhavJhaveri – RaghavPai Nov 26 '21 at 12:01
Swift 5
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")
}
}
}

- 1,656
- 19
- 19
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")
}
}
}
}

- 228
- 3
- 12
-
-
where you have mention the number on which i have to send the message???? – user9483522 Nov 06 '19 at 16:16
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.

- 29,388
- 11
- 94
- 103

- 523
- 6
- 9
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.

- 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
Swift 5
Please follow the below steps for sharing on WhatsApp through URL Schemes
- Add this code in your app "info.plist"
<key>LSApplicationQueriesSchemes</key>
<array>
<string>whatsapp</string>
</array>
- 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!

- 1,383
- 10
- 19
As per their FAQ, you should be using the universal links instead:
Reference: https://faq.whatsapp.com/563219570998715/?locale=en_US

- 3,718
- 1
- 24
- 38