2012-06-19 124 views
0

我在iPad上的應用程序文檔文件夾中保存了一個pdf文件。我希望用戶使用iBooks在iPad上打開該PDF文件。有沒有辦法打開保存在應用程序的文檔文件夾中的iBooks中的PDF?在iBooks的iPad文檔文件夾中打開保存的PDF文件?

+0

可能重複http://stackoverflow.com/questions/8428352/an -app-to-open-ibook-and-then-this-special-pdf-pub-file-straightaway-in-one-g) – Ilanchezhian

+0

我不想在iBook中打開應用程序我只想打開pdf這是保存在應用程序的文檔文件夾 –

回答

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

[應用程序打開的iBook,然後一氣呵成這個特殊的PDF /酒館文件通俗易懂](的
+0

如何使用此代碼我這樣做是這樣的按鈕單擊pdf保存在文檔文件夾中我可以編寫此代碼的按鈕 –

+0

時,當保存pdf可以使用此代碼或創建一個按鈕來閱讀和使用此代碼 –

+0

感謝將這項工作在模擬器上或不我試圖在模擬器上它顯示錯誤消息和askes下載iBooks pdf閱讀器 –

相關問題