ios-open Word, ppt, PDF, execl document mode

Source: Internet
Author: User

This includes downloading and opening the document: You need to first import the the framework of afnetworking "

First step: Create a view that shows the document: Readviewcontroller

(1) The Code of. h is as follows:

@interface readviewcontroller:uiviewcontroller-(void) Loadofficedata: (NSString *) OfficePath; @end

(2). M code is as follows:

 @interface Readviewcontroller () {UIWebView * _dataview; nsstring* _urlstr;}    @end @implementation readviewcontroller-(void) viewdidload {[Super viewdidload];    Do any additional setup after loading the view. if ([Self respondstoselector: @selector (setautomaticallyadjustsscrollviewinsets:)]) {SELF.AUTOMATICALLYADJUSTSSC        Rollviewinsets = NO;    Self.modalpresentationcapturesstatusbarappearance = NO; } if ([Self respondstoselector: @selector (edgesforextendedlayout)]) {self.edgesforextendedlayout = Uirectedge   None; } Self.navigationItem.leftBarButtonItem = [[Uibarbuttonitem alloc] initwithtitle:@ "Back" Style:uibarbuttonitemstylepl Ain target:self Action: @selector (back)];} -(void) back {[self dismissviewcontrolleranimated:yes completion:nil];} 
Still download the above. M inside-(void) Loadofficedata: (NSString *) officepath{    _urlstr=officepath;        if (!_dataview) {        _dataview=[[uiwebview alloc] Initwithframe:cgrectmake (0,0, Self.view.frame.size.width, Self.view.frame.size.height)];        [Self.view Addsubview:_dataview];            }    _dataview.autoresizingmask=uiviewautoresizingflexibleheight| Uiviewautoresizingflexiblewidth;        Nsurl *url = [[Nsurl alloc] initwithstring:_urlstr];    _dataview.scalespagetofit = YES;    Nsurlrequest *requestdoc = [Nsurlrequest requestwithurl:url];    [_dataview Loadrequest:requestdoc];        }

Step Two: Create a tool class to download and open the document: Yzfiledownloadandreadtool

(1) The Code of YZFileDownloadAndReadTool.h is as follows:

@interface yzfiledownloadandreadtool:nsobject/* Open Document */-(void) OpenDocument: (NSString *) documentpath;//Set simple Interest + ( Yzfiledownloadandreadtool *) Sharemanager; @end

(2) The code of YZFILEDOWNLOADANDREADTOOL.M is as follows:

#import "YZFileDownloadAndReadTool.h" #import "ReaderViewController.h" #import "AFNetworking.h" #import " ReadViewController.h "#define GETFILEINAPPDATA (file) [Nshomedirectory () stringbyappendingpathcomponent:[nsstring stringwithformat:@ "/documents/%@", file]] @interface Yzfiledownloadandreadtool () <readerviewcontrollerdelegate > @end @implementation yzfiledownloadandreadtool+ (Yzfiledownloadandreadtool *) Sharemanager {static    Yzfiledownloadandreadtool *sharemanagerinstance = nil; static dispatch_once_t predicate;    Dispatch_once (&predicate, ^{sharemanagerinstance = [[Self alloc] init];    }); return sharemanagerinstance;} /** * @author jakey * * @brief Download file * * @param paramdic additional post parameters * @param requesturl Request address * @param Savedpath saved in Disk location * @param success Download Successful callback * @param failure download Failure callback * @param progress Real-time download progress callback */-(void) Downloadfilewithopti On: (Nsdictionary *) paramdic withinferface: (nsstring*) Requesturl Savedpath: (nsstring*) Savedpath downloadsuccess: (void (^) (afhttprequestoperation *operation, id responseobject)) success Downloadfailure: (void (^) (afhttprequestoperation *operation, Nserror *error)) Failure PR Ogress: (void (^) (float progress)) progress{//sandbox path//nsstring *savedpath = [Nshomedirectory () stringbyappendingst    ring:@ "/documents/xxx.zip"];    Afhttprequestserializer *serializer = [Afhttprequestserializer serializer];    Nsmutableurlrequest *request =[serializer requestwithmethod:@ "POST" Urlstring:requesturl Parameters:nil Error:nil];    Afhttprequestoperation *operation = [[Afhttprequestoperation alloc]initwithrequest:request];    [Operation Setoutputstream:[nsoutputstream Outputstreamtofileatpath:savedpath Append:no]; [Operation setdownloadprogressblock:^ (Nsuinteger bytesread, Long long totalbytesread, long long        Totalbytesexpectedtoread) {Float p = (float) totalbytesread/totalbytesexpectedtoread;    Progress (P);    NSLog (@ "download:%f", (float) totalbytesread/totalbytesexpectedtoread);        }]; [Operation setcompletionblockwithsuccess:^ (afhttprequestoperation *operation, id responseobject)        {success (operation,responseobject);            NSLog (@ "Download succeeded");                } failure:^ (Afhttprequestoperation *operation, Nserror *error) {success (operation,error);            NSLog (@ "Download Failed");        }]; [Operation start];}    -(void) Downloaddocumentoperation: (NSString *) fileName {nsstring *filepath = Getfileinappdata (fileName);    NSString *tempfilename = [NSString stringwithformat:@ "%@.bak", FileName];        NSString *tempfilepath = Getfileinappdata (tempfilename);    NSLog (@ "----savepath----%@", filePath); #warning URL needs to be modified [self downloadfilewithoption:nil withinferface:@] Http://223.202.51.70/FileServe R/downloadfile/17adc036-24ac-4df8-8a49-90c312c0f300.pdf "Savedpath:filepath Download success:^ (AFHTTPREquestoperation *operation, id responseobject) {[[Nsfilemanager Defaultmanager] Moveitematpath:tempfi                     Lepath Topath:filepath error:nil];//[self opendocument:filepath];                 [Self openhaddownloaddocument:filename]; } downloadfailure:^ (Afhttprequestoperation *operation, Nserror *error) {} progress:^ (Float progress) {                                      }];} /* Open document */-(void) OpenDocument: (NSString *) documentpath{UIWindow *window = [[[UIApplication Sharedapplication] Delega    TE] window);    NSString *filepath = Getfileinappdata (documentpath);            NSString *documentname = [FilePath lastpathcomponent]; if (![        [Nsfilemanager Defaultmanager] Fileexistsatpath:filepath] {[Self downloaddocumentoperation:documentpath];    Return        } Readviewcontroller * Readview=[[readviewcontroller alloc] init]; Uinavigationcontroller *nav = [[UinavigationcontRoller alloc] initwithrootviewcontroller:readview];                [Window.rootviewcontroller Presentviewcontroller:nav Animated:yes Completion:nil];        Readview.navigationitem.title=documentpath;                NSString *documentsdirectory = [Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"]; nsstring* _urlstr=[nsstring stringwithformat:@ "%@/%@", Documentsdirectory,[documentpath                Stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]];    [Readview LOADOFFICEDATA:_URLSTR]; }

step three: In the desired click, pour YZFileDownloadAndReadTool.h, then implement

#pragma mark-click interface to download pdf-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *) event {//    Yzfiledownloadandreadtool *tools = [Yzfiledownloadandreadtool sharemanager];//    [Tools opendocument:@ ' 01- Sensor. pptx "];        [[Yzfiledownloadandreadtool Sharemanager] opendocument:@ "01-sensor. pptx"];//    [Tools opendocument:@ " button rounded settings and border settings. docx "];//    [Tools opendocument:@" Vaudicamps work schedule. xlsx "];//    [Tools opendocument:@" ArcGIS for IOS 2.3 Development Tutorials-Basic edition. pdf "];}

ios-open Word, ppt, PDF, execl document mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.