在iBooks的iPad文档文件夹中打开保存的PDF文件? [英] Open saved PDF file in documents folder of iPad in iBooks?

查看:134
本文介绍了在iBooks的iPad文档文件夹中打开保存的PDF文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将pdf文件保存在iPad上的apps文件夹中。我希望用户使用iBooks在iPad上打开该PDF文件。有没有办法在iBooks中打开保存在应用程序的文档文件夹中的PDF?

I have saved a pdf file in the apps documents folder on the iPad. I want the user to open that PDF file on iPad using iBooks. Is there any way to open the PDF in iBooks which is saved in documents folder of the application?

推荐答案

编辑:最佳选项:使用 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的回答

这篇关于在iBooks的iPad文档文件夹中打开保存的PDF文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆