iphone - Cookies don't work in UIWebView displaying "local content" -
there lot of threads using uiwebview caches and/or cookies, seem relate remote urls.
i cannot cookies work when "displaying local content" (as iphone docs call it).
for example, if load plain old html file bundle resource:
- (void) viewdidload { [super viewdidload]; nsstring* path = [[nsbundle mainbundle] pathforresource:@"index" oftype:@"html"]; nsurl* url = [nsurl fileurlwithpath:path]; nsdata* data = [nsdata datawithcontentsoffile:path]; [web loaddata:data mimetype:@"text/html" textencodingname:@"us-ascii" baseurl:url]; }
then:
- (void) webviewdidfinishload:(uiwebview*)webview { nsstring* result = [web stringbyevaluatingjavascriptfromstring: @"try{document.cookie='name=value';''+document.cookie;}catch(e){''+e}"]; nslog(@"result = '%@'", result); }
results in:
result = ''
setting url actual filename rather directory prevents getting: result = 'error: security_err: dom exception 18'
, cookies not seem persist.
i have found satisfactory work-around. specifying real url, such http://localhost/...
, , intercepting loading, subclassing nsurlcache class, in order fetch actual local content.
- (nscachedurlresponse*) cachedresponseforrequest:(nsurlrequest *)request { nsstring* path = [[request url] path]; nsdata* data = [... content of local file ...]; nsurlresponse *response = [[nsurlresponse alloc] initwithurl:[request url] mimetype:[self mimetypeforpath:path] expectedcontentlength:[data length] textencodingname:nil]; nscachedurlresponse* cachedresponse = [[nscachedurlresponse alloc] initwithresponse:response data:data]; [response release]; return [cachedresponse autorelease]; }
Comments
Post a Comment