html - Link to css file in documents from UIWebView without using Base Path -
i have loaded html string (after downloading css file documents) this:
/* result html file references local resources <link href="file:///users/user/library/developer/coresimulator/devices/123/data/containers/data/application/123/documents/gen1.css" rel="stylesheet" type="text/css"> , external resources <link href="http://api.tiles.mapbox.com/mapbox.js/v1.6.4/mapbox.css" rel="stylesheet" /> */ pagegenerator *generator = [pagegenerator sharedgenerator]; nsstring *result = [generator generatepagewithoptions:options]; // filesdir file:// + [[[nsfilemanager defaultmanager] urlsfordirectory:nsdocumentdirectory indomains:nsuserdomainmask] lastobject] + / [self.webview loadhtmlstring:result baseurl:[nsurl urlwithstring:[generator filesdir]]];
now, problem external resources contain url's "//www.site.com/img.png" , uiwebview rewrites them "file://www.site.com/img.png", , gets 404 error.
how can access local resource without need set base url? or how can rewrite "//" "http://" instead "file://"?
thanks lot.
what want install custom nsurlprotocol creating class subclasses nsurlprotocol type , implements nsurlconnectiondelegate , registering using [nsurlprotocol registerclass:[myurlprotocol class]];
this comprehensive tutorial on doing want do.
anytime webview makes request asset, css file or image, it's going call + (bool)caninitwithrequest:(nsurlrequest *)request
request. can inspect nsurlrequest see if asset has scheme using [request.url.scheme isequaltostring:@"file']
. in scenario, can return yes
protocol delegate can take on request.
you'll want override url used however, (or @ least result of happens it).
i think should able use this:
- (void)startloading { nsurlrequest* defaultrequest = self.request; // request file://... nsmutableurlrequest* request = [defaultrequest mutablecopy]; // change url or whatever want on request here. self.connection = [nsurlconnection connectionwithrequest:request delegate:self]; }
if doesn't work, can @ changing + (nsurlrequest *)canonicalrequestforrequest:(nsurlrequest *)request
return modified request. note result returned should identical given request ios caching mechanisms use nsurlrequest return.
you can add in rest of code shown in link above delegate:
+ (nsurlrequest *)canonicalrequestforrequest:(nsurlrequest *)request { return request; } + (bool)requestiscacheequivalent:(nsurlrequest *)a torequest:(nsurlrequest *)b { return [super requestiscacheequivalent:a torequest:b]; } - (void)stoploading { [self.connection cancel]; self.connection = nil; } - (void)connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response { [self.client urlprotocol:self didreceiveresponse:response cachestoragepolicy:nsurlcachestoragenotallowed]; } - (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data { [self.client urlprotocol:self didloaddata:data]; } - (void)connectiondidfinishloading:(nsurlconnection *)connection { [self.client urlprotocoldidfinishloading:self]; } - (void)connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error { [self.client urlprotocol:self didfailwitherror:error]; }
this example has overridden image requests return pictures of david hasselhoff.
this mechanism powerful. instance, use theme app substituting every css file requested @ runtime without changing html.
Comments
Post a Comment