IOS uses UIDocumentInteractionController and Quick Look to open or preview a document
During App development, we cannot avoid opening software files, such as Excel files and Word files, if you want to open files in different formats such as image files or use third-party apps, you need to use UIDocumentInteractionController and Quick Look to solve these problems.
How to Use UIDocumentInteractionController
Notes for using UIDocumentInterRactionController
How to Use Quick Look
I. How to Use UIDocumentInteractionController
-Create a UIDocumentInteractionController object and initialize a URL for the object as the file path.
1. First, follow UIDocumentInteractionControllerDelegate2, and then create a UIDocumentInteractionController object @ property (nonatomic, retain) UIDocumentInteractionController * docController; 3. initialize the UIDocumentInteractionController object (void) in the method) open {NSString * fileName = [self. listDicobjectForKey: @ "fileName"]; NSString * path = [NSHomeDirectory () Metadata: _ docController = [UIDocumentInteractionController interactionControllerWithURL: [NSURL fileURLWithPath: path]; // initialize a loading path _ docController for this object. delegate = self; // set proxy // preview/[_ docController presentPreviewAnimated: YES]; CGRect navRect = self. navigationController. navigationBar. frame; navRect. size = CGSizeMake (1500366f, 40366f); // display the menu item [_ docController presentOptionsMenuFromRect: navRectinView: self. viewanimated: YES]; // display does not contain preview menu items // [docController presentOpenInMenuFromRect: navRect inView: self. view animated: YES];} 4. Proxy method-(UIViewController *) callback :( UIDocumentInteractionController *) controller {return self;}-(UIView *) documentInteractionControllerViewForPreview :( UIDocumentInteractionController *) controller {return self. view;}-(CGRect) documentInteractionControllerRectForPreview :( UIDocumentInteractionController *) controller {return self. view. frame ;}
Ii. Notes for using UIDocumentInterRactionController
1. When UIDocumentInterRactionController is defined, it must be of the retain type, because the object must be held;
2. When you select to display a menu item that contains a preview
[_ DocController presentOptionsMenuFromRect: navRectinView: self. viewanimated: YES]; note that this method will trigger the built-in printing output of this class and print the log information, which may cause the App to crash seriously, no solution has been found. The output result is similar to the following (the content after the output is omitted here, because there are too many ):
unknown activity items supplied: ( { "public.jpeg" =
When the above method does not meet your requirements, you can consider using Quick Look.
Iii. Usage of Quick Look
1. First, add the QuickLook. FrameWork. I will not explain how to add it.
2. Add the header file in the Controller to be used # import
- (void)open{ QLPreviewController *myQlPreViewController = [[QLPreviewController alloc]init]; myQlPreViewController.delegate =self; myQlPreViewController.dataSource =self; [myQlPreViewController setCurrentPreviewItemIndex:0]; [self presentViewController:myQlPreViewControlleranimated:YEScompletion:nil];
5. Proxy Method
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller{ return 1;}- (id )previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index{ NSString *fileName = [self.listDicobjectForKey:@"fileName"]; NSString* path = [NSHomeDirectory()stringByAppendingPathComponent:[NSStringstringWithFormat:@"Documents/%@",fileName]]; return [NSURLfileURLWithPath:path];}