Reputation: 111
I know there is scheme to communicate with whatsapp like:
NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=Hello%2C%20World!"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
[[UIApplication sharedApplication] openURL: whatsappURL];
}
But i can't find how to open whatsapp with a phone number that don't exist before.
It's for a "contatc us" page and if i can only open whatsapp and can't pre fill a phone number, it's useless. I need to be contacted with whatsapp...
Does what I am trying to do exist ?
Upvotes: 7
Views: 14777
Reputation: 2339
According to the Whatsapp documentation
For Swift 3.0 OR Above, This will open whatsapp chat of a number
Example of Number: "+6599999999"
UIApplication.shared.open(URL(string: "https://wa.me/+6599999999")!)
Upvotes: 0
Reputation: 3023
For Swift 3.0 OR Above, This will open whatsapp chat of a number
Example of Number: "+918798766554"
if let whatappURL = URL(string: "https://api.whatsapp.com/send?phone=\(number)&text=\(msg)"),
UIApplication.shared.canOpenURL(whatappURL)
{
if #available(iOS 10.0, *) {
UIApplication.shared.open(whatappURL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(whatappURL)
}
}
Upvotes: 0
Reputation: 4815
Swift 3.0 open whats app.
in info.plist file add this line
<key>LSApplicationQueriesSchemes</key>
<array>
<string>whatsapp://</string>
</array>
func OpenWhatsApp(_ vc:UIViewController){
let msg = "" // here pass your message
let urlWhats = "https://api.whatsapp.com/send?phone=(Mobileno)&text=\(msg)" // Mobile number that include country code and without + sign .
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed){
if let whatsappURL = URL(string: urlString) {
if UIApplication.shared.canOpenURL(whatsappURL) {
if #available(iOS 10.0, *) {
_ = UIApplication.shared.open(whatsappURL, options: [:], completionHandler: nil)
} else {
// Fallback on earlier versions
UIApplication.shared.canOpenURL(whatsappURL)
}
} else {
// Cannot open whatsapp
AppUtilities.sharedInstance.showAlert(title: "WhatsApp Support", msg: "Your device is not support whats app")
}
}
}
}
Upvotes: 1
Reputation: 3383
For phone number pre-filled And Text
NSURL *whatsappURL = [NSURL URLWithString:@"https://api.whatsapp.com/send?phone=9530670491&text=hello"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
[[UIApplication sharedApplication] openURL: whatsappURL];
}
click here for Swift
Check Link For More Details https://www.whatsapp.com/faq/en/general/26000030
Upvotes: 11
Reputation: 4615
If anyone looking for Swift version
static func triggerWhats(_ vc:UIViewController){
let msg = "Hello! I have a question"
let urlWhats = "whatsapp://send?phone=+6599999999&text=\(msg)"
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed){
if let whatsappURL = NSURL(string: urlString) {
if UIApplication.shared.canOpenURL(whatsappURL as URL) {
_ = UIApplication.shared.open(whatsappURL as URL, options: [:], completionHandler: nil)
} else {
// Cannot open whatsapp
ViewUtil().popAlertView(vc, title: "WhatsApp Support", message: "Please WhatsApp us at +65999999 if you have any questions", option: "Ok")
}
}
}
}
Upvotes: 1
Reputation: 7333
You can find the documentation of that whatsapp API above answer. Adding some more point in that based on my own trial :
For communicating direct with the specific contact you need abid of that contact. ABID is that parameter which is automatically generated by the system when you save the contact. So if you dont have number saved in your contact so no way you can open that from your application .
There is one workaround that I have used. When your application load first time in the device , you save the contact us number to the address book . when number saved successfully you will get ABID in the return . you can use that ABID to invoke messaging with that contact.
NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?abid=123&text=Hello%2C%20World!"];
Upvotes: 1
Reputation: 7876
According to the Whatsapp documentation, you need to actually have the contact in your address book to open a discussion from the url scheme.
So for your question
Does what i try to do exist ?
the answer is: No.
Upvotes: 1