Whatsapp图片共享无法正常工作

问题描述:

我的类实现了 UIDocumentInteractionControllerDelegate ,我有以下属性: @property(nonatomic,retain)UIDocumentInteractionController * documentInteractionController;

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];
}

您好我使用过这段代码但是图片共享无效。 DocumentInteractionController启动一个带有whatsapp图标的屏幕,当我点击它时我的应用程序崩溃。

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.

这是代码正确的方法设置用于写出临时图像的URL:

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];
}