In the app development process, we can not avoid to open the software files, such as: Excel files, Word files, picture files and other files in different formats or want to use a Third-party app to open these files, Then we need to use the Uidocumentinteractioncontroller and quick look to solve these problems.
Several common techniques for sharing content across apps in the iOS system, such as URL Scheme, Airdrop,uidocumentinteractioncontroller, Uiactivityviewcontroller
Uidocumentinteractioncontroller is supported from the iOS 3.2 SDK, which is a direct inherited nsobject.
We will introduce the simple use of uidocumentinteractioncontroller.
Uidocumentinteractioncontroller
1. Show a list of third-party apps that can manipulate the types of documents we share
2. Add additional actions on the basis of the first display list, such as copying, printing, previewing, saving, and so on.
3. Direct presentation of document content with Quick look frame
The core code is as follows:
-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *) Event {Nsurl *url = [[NSBundle
Mainbundle] urlforresource:@ "004" withextension:@ "PNG"];
NSString *DOCU = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) lastObject];
NSString *filepath = [docu stringbyappendingpathcomponent:@ "004.png"];
Nsurl *url = [Nsurl Fileurlwithpath:filepath];
Self.document = [Uidocumentinteractioncontroller Interactioncontrollerwithurl:url];
Self.document.delegate = self;
Do not show optional operation//[Self.document presentOpenInMenuFromRect:self.view.bounds InView:self.view Animated:yes]; Show optional action//can be combined with proxy method Documentinteractioncontrollerviewcontrollerforpreview: Show preview [Self.document presentoptionsme
NuFromRect:self.view.bounds InView:self.view Animated:yes]; }-(Uiviewcontroller *) Documentinteractioncontrollerviewcontrollerforpreview: (UidocumentinteractioncontroLler *) Controller {return self}/** * (void) Documentinteractioncontrollerdiddismissopeninmenu when the file share panel exits: (
Uidocumentinteractioncontroller *) Controller {NSLog (@ "dismiss");} /** * File sharing panel pop-up call/-(void) Documentinteractioncontrollerwillpresentopeninmenu: (Uidocumentinteractioncontroller *
) Controller {NSLog (@ "Willpresentopeninmenu"); /** * Call/-(void) Documentinteractioncontroller when selecting a File Share app: (Uidocumentinteractioncontroller *) Controller WILLB Eginsendingtoapplication: (Nullable NSString *) application {NSLog (@ "Begin send:%@", application);}
The effect is as follows:
Attention points are:
1. The proxy method to implement Uidocumentinteractioncontroller must comply with the Uidocumentinteractioncontrollerdelegate protocol.
The 2.UIDocumentInteractionController property must be decorated with retain or strong, and the controller must hold the object.
3. All two paths in the code can be used.
4. Pop-up panel method one does not show optional operation, one shows optional operation.
5. Direct use of presentpreviewanimated: Method pop-up Preview.
6. Documentinteractioncontrollerviewcontrollerforpreview with proxy method: Show preview operation.
There is another way to directly preview a file: Quicklook, using the following methods:
1. Import Quicklook.framework to the project.
2. Include header files where needed <QuickLook/QuickLook.h> #import
3. Declaring preview Controller
@property (nonatomic, strong) Qlpreviewcontroller *previewcontroller;
4. Compliance with agreements and data sources qlpreviewcontrollerdelegate, Qlpreviewcontrollerdatasource
Core code:
-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *) event {
//Create preview controller
Self.preview = [ [Qlpreviewcontroller alloc] init];
Set proxy and data source
self.preView.delegate = self;
Self.preView.dataSource = self;
[Self.preview setcurrentpreviewitemindex:0];
[Self PresentViewController:self.preView animated:yes completion:nil];
}
#pragma mark Qlpreviewcontrollerdatasource
-(Nsinteger) Numberofpreviewitemsinpreviewcontroller: ( Qlpreviewcontroller *) Controller {return
1;
}
-(id<qlpreviewitem>) Previewcontroller: (Qlpreviewcontroller *) controller previewitematindex: (NSInteger) Index
{
nsstring *docu = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) Lastobject];
NSString *filepath = [docu stringbyappendingpathcomponent:@ "004.png"];
Nsurl *url = [Nsurl Fileurlwithpath:filepath];
return URL;