简体   繁体   中英

how to integrate whatsapp in ios

hi now i am trying Integrate What's app in our app

i have done already integrate Tweet

:-in this app i create two buttons one(chooseImagePressed) button is choose the image form local file and then and then second(tweetButtonPressed) this is post the image to Tweeter

- (IBAction)tweetButtonPressed:(id)sender
{
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
        [tweetSheet setInitialText:@"Look at this nice picture!"];

        [tweetSheet addImage:self.imageView.image];

        [self presentViewController:tweetSheet animated:YES completion:nil];
    }
    else
    {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Error"
                                  message:@"please setup Twitter"
                                  delegate:self
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }
}

- (IBAction)chooseImagePressed:(id)sender
{
    self.pickerController = [[UIImagePickerController alloc] init];

    self.pickerController.delegate = self;
    self.pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentViewController:self.pickerController animated:YES completion:nil];
}

#pragma mark 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
{
    self.imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];

    [self dismissViewControllerAnimated:YES completion:nil];
}

Please give me any idea about the how to integrated what's app into our app

Please tell me this is possible or Not

Thanks

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM