我已将pdf文件保存在iPad上的apps文件夹中。我希望用户使用iBooks在iPad上打开该PDF文件。有没有办法在iBooks中打开保存在应用程序文档文件夹中的PDF文件?
答案 0 :(得分:5)
编辑:最佳选项:使用 UIActivityViewController
//create file path here
NSString *strFileURL = [NSTemporaryDirectory() stringByAppendingPathComponent:@"data.pdf"];
//Check if file path exists or not
BOOL checkExist = [[NSFileManager defaultManager] fileExistsAtPath:strFileURL isDirectory:nil];
if (checkExist)
{
//create NSURL object from string path
NSURL *urlFilePath = [NSURL fileURLWithPath:strFileURL];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[urlFilePath] applicationActivities:nil];
//for iOS8 check
if ( [activityViewController respondsToSelector:@selector(popoverPresentationController)] )
{
//use triggering UI element like say here its button
activityViewController.popoverPresentationController.sourceView = yourBtnSender;
}
//now present activityViewController
[self presentViewController:activityViewController animated:YES completion:NULL];
}
另一个选项使用UIDocumentInteractionController:
//create file path here
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *pdfFilePath [documentsDir stringByAppendingPathComponent:yourPdfFile.pdf];// your yourPdfFile file here
NSURL *url = [NSURL fileURLWithPath:pdfPath];
//create documentInteractionController here
UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:url];
//set delegate
docController.delegate = self;
//provide button's frame from where popover will be lauched
BOOL isValid = [docController presentOpenInMenuFromRect:yourReadPdfButton.frame inView:self.view animated:YES]; // Provide where u want to read pdf from yourReadPdfButton
//check if its ready show popover
if (!isValid)
{
NSString * messageString = [NSString stringWithFormat:@"No PDF reader was found on your device. In order to consult the %@, please download a PDF reader (eg. iBooks).", yourPDFFileTitle];
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:messageString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
使用UIDocumentInteractionControllerDelegate
方法
// UIDocumentInteractionController delegate method
- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller {
NSLog(@"dissmissed");
}
信用转到@Mutix 's answer