Recently encountered in project development to download the document preview requirements of the attachment, here to summarize my implementation method, this article will be accompanied by the demo I wrote
Here I summarize three implementation methods (1) with WebView Preview (2) through Uidocumentinteractioncontroller implementation jump (3) Apply the Quick Look system framework, the following in turn describes the implementation of each method
First of all, use webview This is more commonly used, do not do too much explanation, the code is as follows:
_webview = [[UIWebView alloc]initwithframe:self.view.bounds]; _webview.delegate = self; Nsurlrequest *request = [Nsurlrequest requestwithurl:_url1]; [_webview loadrequest:request]; [_webview Setscalespagetofit:yes]; [Self.view Addsubview:_webview];
The second application Uidocumentinteractioncontroller implementation method is as follows:
Initialize the object first, and set the popup mode _documentint = [Uidocumentinteractioncontroller interactioncontrollerwithurl:_url2]; [_documentint setdelegate:self]; [_documentint Presentpreviewanimated:yes]; [_documentint presentoptionsmenufromrect:cgrectmake (0, 0, 375, 667) InView:self.view animated:yes];// Then implement the appropriate proxy method-(uiviewcontroller*) Documentinteractioncontrollerviewcontrollerforpreview: ( uidocumentinteractioncontroller*) controller{ return self;} -(uiview*) Documentinteractioncontrollerviewforpreview: (uidocumentinteractioncontroller*) controller{ return Self.view;} -(CGRect) Documentinteractioncontrollerrectforpreview: (uidocumentinteractioncontroller*) controller{ return Self.view.frame;} Called when the "done" button of the preview window is clicked-(void) Documentinteractioncontrollerdidendpreview: (uidocumentinteractioncontroller*) _ controller{ [_documentint dismisspreviewanimated:yes];}
The third kind of quick look, with this method needs to import the Quicklook.framework framework, the code is as follows:
Initialize object Qlpreviewcontroller *myqlpreviewcontroller = [[Qlpreviewcontroller alloc]init]; Myqlpreviewcontroller.delegate =self; Myqlpreviewcontroller.datasource =self; [Myqlpreviewcontroller setcurrentpreviewitemindex:0]; [Self Presentviewcontroller:myqlpreviewcontroller animated:yes completion:nil];//Implementing Proxy Method-(Nsinteger) Numberofpreviewitemsinpreviewcontroller: (Qlpreviewcontroller *) controller{ return 1;} -(ID) Previewcontroller: (Qlpreviewcontroller *) controller previewitematindex: (nsinteger) index{ // NSString *filename = [self.listdic objectforkey:@ "FileName"]; nsstring* Path = [Nshomedirectory () stringbyappendingpathcomponent:[nsstringstringwithformat:@ "Documents/%@ ", FileName]]; return _url3; }
Demo:https://github.com/zk1947/zkdemoall
iOS opens Word document, PPT, etc. in project summary