在iBooks的iPad文档文件夹中打开保存的PDF文件?

The*_*per 0 iphone xcode

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

Par*_*iya 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];

}
Run Code Online (Sandbox Code Playgroud)

另一个选项使用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];
}
Run Code Online (Sandbox Code Playgroud)

使用UIDocumentInteractionControllerDelegate方法

// UIDocumentInteractionController delegate method
- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller {
   NSLog(@"dissmissed");
}
Run Code Online (Sandbox Code Playgroud)

幸得@Mutix的答案