WhatsApp图像共享不起作用

我的类实现了UIDocumentInteractionControllerDelegate ,我有以下属性: @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的图标,当我点击它我的应用程序崩溃。

以下是用正确方法设置用于写出临时映像的URL的代码:

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

要使用WhatsApp共享图像,请使用下面的代码。 这工作正常。

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

对于我来说,当我使用documentInteractionController作为属性时,问题得到了解决。 早些时候我在共享方法中使用它作为局部variables。