iosobjective-cxcodesharewhatsapp

Share image ant text WhatsApp API iOS


I'm trying to share an image and a text with the API of WhatsApp but I'm unable to do it. To share an image I have this code:

if([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"whatsapp://app"]])
{
    NSURL *documentURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
    NSURL *tempFile = [documentURL URLByAppendingPathComponent:@"whatsAppTmp.wai"];
    NSData *imageData = UIImageJPEGRepresentation(imageFinal, 1.0);
    [imageData writeToURL:tempFile options:NSDataWritingAtomic error:nil];
    
    documentController = [UIDocumentInteractionController interactionControllerWithURL:tempFile];
    documentController.UTI = @"net.whatsapp.image";
    documentController.delegate = self;
    
    [documentController presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES];
}

This works fine, but I don't find the method to share with this image a text. I have found how to share a text with the same form in Instagram, like that

documentController.annotation = @{@"InstagramCaption": "text"};

but it doesn't serve to share an image in WhatsApp, and I don't know what key I have to use in the dictionary for that. I know that in Android this can be done with an Intent and the extra EXTRA_TEXT, so I guess in iOS is also possible.

Can anyone help me?


Solution

  • You won't be able to share text and image at the same time in whatsapp.

    If you want to share text, you can do this -

    NSString * mymsg = @"this is test message";
    NSString * urlString = [NSString stringWithFormat:@"whatsapp://send?text=%@",mymsg];
    NSURL * url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    if ([[UIApplication sharedApplication] canOpenURL: url]) {
        [[UIApplication sharedApplication] openURL: url];
    } else {
        //show alert
    }