I am using UIDocumentInteractionController
to share a document (PDF) with other apps.
The view with the different apps shows up just fine but when I press any of the apps to open/save/share the file, my app crashes.
I get the Unrecognised selector sent to instance
error.
It seems that the error comes from the file url I give to (not sure tho):
[UIDocumentInteractionController interactionControllerWithURL:url];
DocumentInteractionManager.h:
@interface DocumentInteractionManager : UIViewController <UIDocumentInteractionControllerDelegate>
@property (nonatomic, retain) UIDocumentInteractionController* documentInteractionController;
@property (nonatomic, copy) NSURL *url;
- (void)openDocumentFromRect:(UIView*)view withURL:(NSURL*)fileUrl;
@end
DocumentInteractionManager.m:
@implementation DocumentInteractionManager
@synthesize documentInteractionController;
@synthesize url;
//fileUrl is something like: file:///var/mobile/Containers/Data/Application/[APP]/Library/file.pdf
- (void)openDocumentFromRect:(UIView*)view withURL:(NSURL*)fileUrl {
self.url = fileUrl;
if (url) {
documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:self.url];
// Configure Document Interaction Controller
[documentInteractionController setDelegate:self];
CGRect rect = [view frame];
[documentInteractionController presentOptionsMenuFromRect:[view frame] inView:view animated:YES];
}
}
@end
EDIT:
Here is how I am creating the url: [NSURL fileURLWithPath:pdfPath]
Note that it works when I use UIActivityViewController
and share via AirDrop
EDIT2:
Here are some of the different messages I get (depending on which app I choose):
[__NSCFString URL]: unrecognized selector sent to instance
Any Ideas ?
Okay, I've figured it out.
What I was doing wrong was that I was instantiating UIDocumentInteractionController
in the - (void)openDocumentFromRect:(UIView*)view withURL:(NSURL*)fileUrl
method. So basically when I was out of that method UIDocumentInteractionController
was destroyed.
I now instantiate UIDocumentInteractionController
in the init method and voilà it works as expected.