Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIDocumentInteractionController OpenInMenu crashes iOS app

Tags:

ios

I'm trying to send an image generated in my app to other apps using the UIDocumentInteractionController OpenIn Menu. I'm saving the UIImage to disk with this code:

    fileJpeg = [NSTemporaryDirectory() stringByAppendingPathComponent:@"activeImage.jpg"];
    jpegFileURL = [NSURL fileURLWithPath:fileJpeg];

    UIImage *imageToWrite = image;

    [UIImageJPEGRepresentation(imageToWrite, 1.0) writeToFile:fileJpeg atomically:YES];

I'm accessing the jpegFileURL in another method to send the image by email using the MFMailComposeViewController, and it works perfectly, so the NSURL is valid. But when I try to send the image to another app (Just sending, I'm not implementing any preview functionality) the app crashes. Here is the method:

- (IBAction)openInOtherApp:(id)sender{

UIDocumentInteractionController *controller = [UIDocumentInteractionController interactionControllerWithURL: jpegFileURL];
controller.delegate = self;
CGRect rect = self.view.frame;
[controller presentOpenInMenuFromRect:rect inView:self.view animated:YES];
}

The Open In menu is presented. When I tap the button of any available app it crashes. When testing in iOS6 (6.0.1) and iOS5 (5.1.1) devices I get no error output in the console (just the usual EXC_BAD_ACCESS (code=1, address... crash), but on a iOS 4.3 device (The app is 4.3 upwards compatible) I get this error in the console:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType actionSheet:clickedButtonAtIndex:]: unrecognized selector sent to instance 0x16b7f0'

I have been reading Apple's documentation on UIDocumentInteractionController and UIDocumentInteractionControllerDelegate, that I implement in my class @interface, but none of the optional delegate methods seem to be required for my needs or helpful in this crash.

Can't figure what is wrong or missing. Any help would be appreciated.

like image 242
romeup Avatar asked Nov 15 '12 12:11

romeup


1 Answers

Found the problem, thanks to this answer. It was a memory thing. I was using the UIDocumentInteractionController as a local instance variable and ARC was releasing it to soon. Turned it into a class property and now everything works.

like image 182
romeup Avatar answered Oct 19 '22 23:10

romeup