0

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

4

1 回答 1

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的回答

于 2012-06-19T04:53:48.110 回答