1

My class implements the UIDocumentInteractionControllerDelegate and I have the following property:@property (nonatomic,retain) UIDocumentInteractionController * documentInteractionController;

if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"whatsapp://app"]]) {
    UIImage     *iconImage = [UIImage imageNamed:@"bg_iPhone_5.jpg"];
    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];
}

Hi I have used this code But Image sharing is not working. DocumentInteractionController launches a screen with whatsapp icon and when I click on it my app crashes.

rckoenes
  • 69,092
  • 8
  • 134
  • 166
Praveen
  • 45
  • 1
  • 11
  • launchServices: invalidationHandler called. I get this in Debug area. – Praveen Dec 09 '14 at 14:08
  • Did you debug the code, like made sure the image is written? Since you not checking this is at all. Also your for getting the document directory, adding to the `NSHomeDirectory()` is not really correct. use `URLForDirectory:inDomain:appropriateForURL:create:error:` – rckoenes Dec 09 '14 at 14:09
  • Can you give me the exact way of using NSHomeDirectory please. – Praveen Dec 09 '14 at 14:14
  • See my answer, this should help you. If any of the `NSLog` is trigged you will get a the error logged in you debug output and this will tell you more about where you can find you issue – rckoenes Dec 09 '14 at 14:52
  • I put a solution here http://stackoverflow.com/questions/8354417/share-image-text-through-whatsapp-in-an-ios-app/20601051#20601051 – Wagner Sales Feb 22 '15 at 17:16

3 Answers3

2

Here is code with the correct way to setup the URL for writing out the temp image:

if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"whatsapp://app"]]) {
    NSError *error = nil;
    NSURL *documentURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:&error];
    if (!documentURL) {
        NSLog(@"Error getting doucment directory: %@", error);
    }

    NSURL *tempFile = [documentURL URLByAppendingPathComponent:@"whatsAppTmp.wai"];
    UIImage *iconImage = [UIImage imageNamed:@"bg_iPhone_5.jpg"];

    NSData *imageData = UIImageJPEGRepresentation(iconImage, 1.0) ;
    if (![imageData writeToURL:tempFile options:NSDataWritingAtomic error:&error]) {
        NSLog(@"Error writing File: %@", error);
    }

    self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:tempFile];
    self.documentInteractionController.UTI = @"net.whatsapp.image";
    self.documentInteractionController.delegate = self;
    [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];
}
rckoenes
  • 69,092
  • 8
  • 134
  • 166
2

For sharing Image using WhatsApp, use the below Code. It's working fine.

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

        UIImage     * iconImage = [UIImage imageNamed:@"my_account.png"];
        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];
    }
0

For me the issue got fixed when I used documentInteractionController as a property. Earlier I was using that as a local variable in the share method.

Krishna Kumar
  • 187
  • 1
  • 17