ios - Saved PDF to Disk; won't Load in WebView Offline -
i can't wrap head around issue here: save pdf disk - load try load pdf on physical device , turn on airplane mode. file doesn't load. can me ? it's issue of saving , retrieving same directory.
save location returns:
save location : /var/mobile/containers/data/application/a1f1b294-9282-483b-b2e1-76c586a9631e/documents/fileurl.pdf
loading directory returns :
filepath : /var/mobile/containers/data/application/a1f1b294-9282-483b-b2e1-76c586a9631e/documents/var/mobile/containers/data/application/a1f1b294-9282-483b-b2e1-76c586a9631e/documents/fileurl.pdf
saving pdf:
pdfviewcontroller.m
- (ibaction)download:(id)sender { nsmanagedobjectcontext *context = [self managedobjectcontext]; nserror *error = nil; nsfetchrequest *request = [nsfetchrequest fetchrequestwithentityname:@"downloads"]; [request setpredicate:[nspredicate predicatewithformat:@"pubnumber = %@", self.title]]; [request setfetchlimit:1]; nsuinteger count = [context countforfetchrequest:request error:&error]; if (count == nsnotfound) { } else if (count == 0) { nsdata *pdfdata = [[nsdata alloc] initwithcontentsofurl:[ nsurl urlwithstring:self.pdfurl]]; nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring* filepath = [documentsdirectory stringbyappendingpathcomponent:self.pdfurl]; [pdfdata writetofile:filepath atomically:yes]; nslog(@"save location : %@", filepath); nsmanagedobject *newdwnld = [nsentitydescription insertnewobjectforentityforname:@"downloads" inmanagedobjectcontext:context]; [newdwnld setvalue:self.title forkey:@"pubnumber"]; [newdwnld setvalue:titlestring forkey:@"pubtitle"]; [newdwnld setvalue:filepath forkey:@"puburl"]; if (![context save:&error]) { uialertview *alertview = [[uialertview alloc] initwithtitle:@"can't save" message:@"error handeling request" delegate:self cancelbuttontitle:@"ok" otherbuttontitles:nil, nil]; alertview.tag = 2; [alertview show]; } } else { uialertview *alertview = [[uialertview alloc] initwithtitle:@"stand fast!" message:@"this document in downloads library" delegate:self cancelbuttontitle:@"ok" otherbuttontitles:nil, nil]; alertview.tag = 2; [alertview show]; } }
loading pdf:
downloadstableviewcontroller.m
#import "pdfviewcontroller.h" @interface downloadstableviewcontroller () @property (strong) nsmutablearray *downloads; @property (weak) nsstring *filepath; @end -(void)viewdidappear:(bool)animated { [[nsuserdefaults standarduserdefaults] setbool:no forkey:@"dwnldview"]; nsmanagedobjectcontext *managedobjectcontext = [self managedobjectcontext]; nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] initwithentityname:@"downloads"]; self.downloads = [[managedobjectcontext executefetchrequest:fetchrequest error:nil] mutablecopy]; self.filepath = [self.downloads valueforkey:@"puburl"]; [self.tableview reloaddata]; } - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { uitableviewcell *cell = [tableview cellforrowatindexpath:indexpath]; [[nsuserdefaults standarduserdefaults] setbool:yes forkey:@"dwnldview"]; uistoryboard *storyboard = [uistoryboard storyboardwithname:@"main" bundle:nil]; pdfviewcontroller *pdfvc = [storyboard instantiateviewcontrollerwithidentifier:@"pdfviewcontroller"]; pdfvc.title = cell.textlabel.text; pdfvc.dwnldspassedurl = self.filepath; pdfvc.modaltransitionstyle = uimodaltransitionstylecrossdissolve; pdfvc.modalpresentationstyle = uimodalpresentationfullscreen; [self presentviewcontroller:pdfvc animated:yes completion:nil]; }
pdfviewcontroller.h
#import "mbprogresshud.h" #import <coredata/coredata.h> #import <parse/parse.h> #import <messageui/messageui.h> <uiwebviewdelegate, uialertviewdelegate, mfmailcomposeviewcontrollerdelegate> @property (weak, nonatomic) nsstring *dwnldspassedurl;
pdfviewcontroller.m
- (void)viewdidload { if ([[dwnlded objectforkey:@"dwnldview"] isequal:@yes]) { [self localquery]; } } - (void)localquery { nslog(@"local query initiated"); nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring* filepath = [documentsdirectory stringbyappendingpathcomponent:dwnldspassedurl]; nslog(@"filepath : %@", filepath); nsurl *loadurl = [nsurl fileurlwithpath:filepath]; nsurlrequest *localdoc = [nsurlrequest requestwithurl:loadurl]; [self.webview loadrequest:localdoc]; }
check filepath
in (void)localquery
think it's not true
downloadstableviewcontroller.m
tableview:didselectrowatindexpath:
set dwnldspassedurl = self.filepath
pdfviewcontroller.m
nsstring* filepath = [documentsdirectory stringbyappendingpathcomponent:dwnldspassedurl];
can't true document filepath
//documentsdirectory : /var/mobile/containers/data/application/a1f1b294-9282-483b-b2e1-76c586a9631e/documents/ //dwnldspassedurl: /var/mobile/containers/data/application/a1f1b294-9282-483b-b2e1-76c586a9631e/documents/fileurl.pdf
try this link
edit: solution:
pdfviewcontroller.m
use nsstring* filepath = dwnldspassedurl;
instead nsstring* filepath = [documentsdirectory stringbyappendingpathcomponent:dwnldspassedurl];
or
downloadstableviewcontroller.m tableview:(uitableview *)tableview didselectrowatindexpath:
pdfvc.dwnldspassedurl = yourfilename;
Comments
Post a Comment