Open local or network PDF, word, Excel, PPT and other files (available in a variety of ways)

Previous projects have requested preview of PDF files, local and network, so study. Open the PDF Word Excel PPT files are generally two ways, QuickLook framework and UIDocumentInteractionController, this article mainly introduces two kinds of open, open and open as well as local network in two ways. Plug in the demo download link:

Let’s look at the use of UIDocumentInteractionController first.

Self.documentInteractionController [UIDocumentInteractionController = interactionControllerWithURL:URL]; self.documentInteractionController.delegate = self; [self.documentInteractionController presentPreviewAnimated:YES];

The agent method of UIDocumentInteractionControllerDelegate.

#pragma mark -UIDocumentInteractionControllerDelegate proxy method / / this is what must be achieved, otherwise it will not display (UIViewController *) documentInteractionControllerViewControllerForPreview: (* UIDocumentInteractionController) controller{return self;} / / set the preview name - (void) documentInteractionControllerWillBeginPreview: (UIDocumentInteractionController * controller{) = @ "attachment preview";} / / Done end click Preview - (void) documentInteractionControllerDidEndPreview: (UIDocumentInteractionController * controller{)}

Look at how the local file is opened in the QuickLook framework. Import this framework when using: #import, &lt, QuickLook/QuickLook.h>.

#import < QuickLook/QuickLook.h> @interface; QuickLookViewController (<); QLPreviewControllerDataSource, QLPreviewControllerDelegate> @property (nonatomic, strong) QLPreviewController *previewController; @end @implementation QuickLookViewController (void viewDidLoad) {[super viewDidLoad]; self.view.backgroundColor [UIColor = whiteColor]; self.title = @ "attachment preview"; _previewController [[QLPreviewController alloc] = init]; _previewController.dataSource = self; _previewController.delegate = self; _previewController.view.frame = CGRectMake (0, 64, self.view.frame.size.width, self.view.frame.size.height); //_previewController.currentPreviewItemIndex = 2; [self addChildViewController: _previewCon Troller]; [self.view addSubview:_previewController.view]; [_previewController reloadData];} #pragma mark agent (ID < QLPreviewItem> method;) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index{return self.fileURL;} / / - (NSInteger) numberOfPreviewItemsInPreviewController: must implement (QLPreviewController *) controller{return 1;}

Finally, take a look at how to download based on webView, if the download is successful, it is based on local open, automatically cache PDF, word, Excel, PPT and other documents.

- (void) viewDidLoad viewDidLoad] self.view.backgroundColor {[super; [UIColor = whiteColor]; _openFileWebView = [[UIWebView alloc]initWithFrame:CGRectMake (0, 64, self.view.frame.size.width, 0); _openFileWebView.delegate = self; _openFileWebView.backgroundColor = [UIColor cyanColor] [_openFileWebView; loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.fileURLString]]];}

WebView proxy method.

#pragma mark - webView - (BOOL) webView: agent (UIWebView *) webView shouldStartLoadWithRequest: (NSURLRequest *) request navigationType: (UIWebViewNavigationType) navigationType{return YES;} - (void) webViewDidFinishLoad: (UIWebView *) webView{NSLog (@ "network load"); NSURL *targetURL = [NSURL URLWithString:self.fileURLString]; NSString *docPath [self = documentsDirectoryPath]; NSString = *pathToDownloadTo [NSString stringWithFormat:@ "%@/%@", docPath, [targetURL lastPathComponent]]; NSFileManager *fileManager = [NSFileManager defaultManager]; / / BOOL hasDownLoad= [fileManager if you have already downloaded fileExistsAtPath:pathToDownloadTo]; if (hasDownLoad) {NSLog (@ "download"); self.fileURL = [NSURL fi LeURLWithPath:pathToDownloadTo] _qlVC; alloc]init] = [[QLPreviewController; _qlVC.delegate = self; _qlVC.dataSource = self; [self presentViewController:_qlVC animated:NO completion:nil];}else {NSLog (@ "didn't download"); NSURL *targetURL = [NSURL URLWithString:self.fileURLString]; NSData *fileData = [[NSData alloc] initWithContentsOfURL:targetURL]; NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; [fileData writeToFile:[NSString stringWithFormat:@, documentsDirectory, [targetURL "%@/%@" lastPathComponent]] atomically:YES]; NSURLRequest *request = [N SURLRequest requestWithURL:targetURL]; [_openFileWebView loadRequest:request];}

QLPreviewController agent.

- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController * controller) {return 1} - (ID; < QLPreviewItem> previewController:) (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index {return self.fileURL;}