您可以使用以下代码打开 PDFUIDocumentInteractionController
这是一个显示 pdf 的示例,NSBundle
您也可以从 Document 目录显示,就像我们显示图像一样。您需要完整的文档路径并将其转换为NSURL
使用fileURLWithPath
:-
在yourViewController.h文件中:-
@interface yourViewController : UIViewController<UIDocumentInteractionControllerDelegate>
{
UIDocumentInteractionController *documentationInteractionController;
}
在 **yourViewController.m文件中:-**
-(void) viewDidAppear:(BOOL)animated {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"abcd.pdf"];
NSURL *pdfUrl = [NSURL fileURLWithPath:filePath];
documentationInteractionController = [UIDocumentInteractionController interactionControllerWithURL:pdfUrl];
documentationInteractionController.delegate = self;
[documentationInteractionController presentPreviewAnimated:YES];
[super viewDidAppear:animated];
}
代表UIDocumentInteractionController
- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller {
return self;
}
演示链接
快乐编码... :)