how to integrate whatsapp in ios

旧时模样 提交于 2019-12-01 13:07:31

No, It's not possible as like tweeter and Facebook api. But you can send message from your app to whatsapp if whatsapp is already installed as below

NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=Hello%2C%20World!"];//use this method stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding to convert it with escape char
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
    [[UIApplication sharedApplication] openURL: whatsappURL];
}

But If you want to share document like files, images, video, you've to send it via UIDocumentInteractionController.

Note: whatsapp should be installed for above two, otherwise you can't do anything as you like. See this for current whatsApp doc.

Simple integration

   NSURL *whatsappURL = [NSURL URLWithString:@"https://api.whatsapp.com/send?phone=9530670491&text=hello"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
    [[UIApplication sharedApplication] openURL: whatsappURL];
}

Swift

var whatsappURL = URL(string: "https://api.whatsapp.com/send?phone=9530670491&text=hello")
if UIApplication.shared.canOpenURL(whatsappURL) {
UIApplication.shared.openURL(whatsappURL!)
}

Also check this link https://www.whatsapp.com/faq/en/general/26000030

You will get more inputs here:

http://www.whatsapp.com/faq/en/iphone/23559013

-This is used to share any Image/vodeo with WhatsApp. -You need to do UIDocumentInteractionController Class Reference in your code. -You need to save the image to disk, and then create a UIDocumentInteractionController with that file URL. -Following are the code snaps for the same and you can share image with WhatsApp.

  //Path of the image which is present in bundle 
    NSString* path = [[NSBundle mainBundle] pathForResource:@"images" ofType:@"jpg”];

  /* here you can also give the path of image which is saved on disk.*/


       if (path) {
            NSURL* url = [NSURL fileURLWithPath:path];
            UIDocumentInteractionController* docController = [UIDocumentInteractionController interactionControllerWithURL:url];
            docController.delegate = self;
            [docController presentPreviewAnimated:YES];
        }

For text sharing

 //This is sharing text encoding with NSUTF8StringEncoding
    NSString* strSharingText = [txtWhatsApp.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    //This is whatsApp url working only when you having app in your Apple device
    NSURL *whatsappURL = [NSURL URLWithString:[NSString stringWithFormat:@"whatsapp://send?text=%@",strSharingText]];

   if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
        [[UIApplication sharedApplication] openURL: whatsappURL];
    }

I prefer this documented method:

if let urlString = "https://wa.me/\(whatsappPhoneNumber)/?text=Hi. ".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed), 
    let url = URL(string: urlString), 
    UIApplication.shared.canOpenURL(url) {
        UIApplication.shared.open(url)
}

For Swift 4.2 and above

let whatsAppURL = URL(string: "https://api.whatsapp.com/send?phone=0123456")

if UIApplication.shared.canOpenURL(whatsAppURL!)
{
    UIApplication.shared.open(whatsAppURL!, options: [:], completionHandler: nil)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!