1

I have tried this code but its showing only whatsapp icon, but not sending content on it.Kindly suggest what's wrong I am doing

- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL*) fileURL usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate {

    self.documentationInteractionController = [UIDocumentInteractionController interactionControllerWithURL: fileURL];
    self.documentationInteractionController.delegate = interactionDelegate;
    return self.documentationInteractionController;
}


-(void)btnClick{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"]; //here i am fetched image path from document directory and convert it in to URL and use bellow


    NSURL *imageFileURL =[NSURL fileURLWithPath:getImagePath];
    NSLog(@"imag %@",imageFileURL);

    self.documentationInteractionController.delegate = self;
    self.documentationInteractionController.UTI = @"net.whatsapp.image";
    self.documentationInteractionController = [self setupControllerWithURL:imageFileURL usingDelegate:self];
    [self.documentationInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];

}

1 Answers1

1

For send a message text on whatsapp:

        NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=Test%20message%20whatsapp!"];

        if ([[UIApplication sharedApplication] canOpenURL:whatsappURL]) {
            [[UIApplication sharedApplication] openURL: whatsappURL];
        } else {
            UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Your device doesn't support Whatsapp!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [warningAlert show];
        }

Edit: And image:

    if ([[UIApplication sharedApplication] canOpenURL: [NSURL      URLWithString:@"whatsapp://app"]]){

UIImage     * iconImage = [UIImage imageNamed:@"YOUR IMAGE"];
NSString    * savePath  = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/whatsAppTmp.wai"];

[UIImageJPEGRepresentation(iconImage, 1.0) writeToFile:savePath atomically:YES];

_documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
_documentInteractionController.UTI = @"net.whatsapp.image";
_documentInteractionController.delegate = self;

[_documentInteractionController presentOpenInMenuFromRect:CGRectMake(0, 0, 0, 0) inView:self.view animated: YES];


    } else {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"WhatsApp not installed." message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
    }

Look on whatsapp: http://www.whatsapp.com/faq/en/iphone/23559013 and this response: Share image/text through WhatsApp in an iOS app If you want there is a ShareKit with Whatsapp integration: https://github.com/heringb/ShareKit

Community
  • 1
  • 1
weso
  • 189
  • 3
  • 14
  • Thanks for quick reply i appreciate your support. Above code worked fine for sharing text, but i need to share url so do i need to convert NSString to some html type of URL. ShareKit have some missing files, its giving so many errors. – Aanchal Chaurasia Jan 20 '15 at 07:16
  • 1
    You can share a link in a message text: NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=Download%20my%20application%20here:%20http://itunes.apple.com/app/id000000191"]; – weso Jan 20 '15 at 11:03