Reputation: 563
I am quite desperate with UIDocumentInteractionController
. The only thing I want to do is to share my pdf file from my app with all the other apps that are registered for PDF.
This is my code:
NSString * filePath = [[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"pdf"];
self.docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
self.docController.delegate = self;
NSURL *url = [NSURL fileURLWithPath:filePath];
BOOL isValid = [[UIApplication sharedApplication] canOpenURL:url];
NSLog(@"uti: %@", [self.docController UTI]);
//[self.docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
[self.docController presentOpenInMenuFromBarButtonItem:self.backBut animated:YES];
isValid
variable is always TRUE, UTI logged as com.adobe.pdf (which seems to be correct) but calling presentOpenFrom...
both calls (one commented) always returns NO.
I am testing with simulator, is that the problem? Does anybody see something wrong with the code or has any idea what to check?
Upvotes: 0
Views: 6160
Reputation: 133
If you are dealing with PDF file then you should have any application capable of opening PDF file on your device (simulator).
You may want to install PDF File Viewer because it's open source.
After you install the app by running from Xcode your UIDocumentInteractionController
will appear correctly.
Here is a sample code for downloading any document from the web and presenting UIDocumentInteractionController
for passing it to another application.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
BOOL isDir = NO;
NSError *error;
if (! [[NSFileManager defaultManager] fileExistsAtPath:cachePath isDirectory:&isDir] && isDir == NO)
{
[[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:NO attributes:nil error:&error];
}
NSString *filePath = [cachePath stringByAppendingPathComponent:url.lastPathComponent];
NSData *fileData = [NSData dataWithContentsOfURL:url];
[fileData writeToFile:filePath atomically:YES];
NSURL *filePathURL = [NSURL fileURLWithPath:filePath];
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:filePathURL];
[self.documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
Upvotes: 1
Reputation: 1
Kenney's solution remains true IF all that's needed for this implementation is a preview of the PDF. However, it does not solve the original problem of presentOpenInMenuFromRect or presentOpenInMenuFromBarButtonItem. This will only present the pdf in a preview rather than an actual "Open In..." dialog. There is no known solution besides using a device to bring up the Open In controller.
The presentOpenInMenu... method checks with the system to see if there are any registered PDF readers downloaded. Since the iOS simulator does not provide one and there's no way of adding one, this functionality can only be tested on a device.
Upvotes: 0
Reputation: 1
Try this:
NSString* path = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"pdf"];
if (path) {
NSURL* url = [NSURL fileURLWithPath:path];
UIDocumentInteractionController* docController = [UIDocumentInteractionController interactionControllerWithURL:url];
docController.delegate = self;
[docController presentPreviewAnimated:YES];
}
Upvotes: 0