Tuesday, 15 February 2011

ios - Keep losing php session cookie in UIWebView -



ios - Keep losing php session cookie in UIWebView -

i'm new forum , quite new ios dev. i'm having problem uiwebview, maintain losing login permission html form sets phpsession cookie (with expiration set 8h, works on desktop). seems uiwebview throws cookie away after hr or so, instead of 8h. i've read nshttpcookiestorage should take care automatically cookies, after app enters background mode or quits.

the nshttpcookie looks this

nshttpcookie version:0 name:"phpsessid" value:"6f267fdc94c1ce5fcsdgg49b59a8f46b" expiresdate:2013-02-21 01:27:58 +0000 created:2001-01-01 00:00:01 +0000 (1) sessiononly:false domain:"mydomain.com" path:"/" issecure:false

and save on exit/sleep nsuserdefaults , load 1 time again when coming foreground/opening app, recommended here: how set web view loaded login user -iphone - still, maintain losing login.

can point me in direction? lot!

i doing (according post underneath):

nsurl *lasturl = [[self.webview request] maindocumenturl]; if (lasturl.absolutestring == null) { lasturl = [nsurl urlwithstring:@"http://mydomain.com/"]; } nsarray *cookiesfordomain = [[nshttpcookiestorage sharedhttpcookiestorage] cookiesforurl:[nsurl urlwithstring:@"http://mydomain.com"]]; nsmutableurlrequest *newrequest = [nsmutableurlrequest requestwithurl:lasturl]; (nshttpcookie *cookie in cookiesfordomain) { nsstring *cookiestring = [nsstring stringwithformat:@"%@=%@", [cookie name], [cookie value]]; [newrequest setvalue:cookiestring forhttpheaderfield:@"cookie"]; nslog(@"inserted cookie request: %@", cookie); } [self.webview loadrequest:newrequest];

i utilize in app many requests , every time when send request cookies url nshttpcookiestorage. not utilize nsuserdefaults or else store cookies.

when send new request set needed cookies self

nsurl *myurl = .... nsmutablerequest *mutablerequest = .... nsarray *cookiestoset = [[nshttpcookiestorage sharedhttpcookiestorage] cookiesforurl:myurl]; (nshttpcookie *cookie in cookiestoset) { [cookiestringtoset appendformat:@"%@=%@;", cookie.name, cookie.value]; } if (cookiestringtoset.length) { [mutablerequest setvalue:cookiestringtoset forhttpheaderfield:@"cookie"]; }

and works

ios objective-c xcode cookies uiwebview

No comments:

Post a Comment