ios - NSHTTPCookieStorage loosing cookies if app starts before phone has been unlocked at least once after a reboot -
problem
as title says, i'm experiencing issue nshttpcookiestorage
loosing cookies our app when it's initialised in background before phone has been unlocked @ least once after reboot.
once cookies lost, can't recovered in way, forcing user re login retrieve new set of cookies , recover session.
if app activity registered first time after user has unlocked her/his phone, works charm.
context:
this issue happened using
nsurlrequest
,nsurlsession
using asihttp , afnetworking automatic cookie handling, came conclusion affecting wholenshttpcookiestorage
class.our app has slc (significant location change monitoring) its
triggered in background automatically.
steps reproduce
- make demo app performs network call ("lets call call a"), authenticate server, getting cookies in response that.
- perform second call (lets call "call b") requires cookies sent in order work. if goes well, cookies should automatically managed , sent server accordingly.
- make demo app able execute background stuff, example enable slc monitoring. in execution of background behaviour perform "call b" again (if it's inside background task or not doesn't matter test purpose)
- reboot phone , don't unlock it.
wait until slc, or background behaviour chose triggered , call b done.
tip: can disable , enable airplane mode force slc trigger on device.
problem: if in time significant location change triggered, app looses cookies forever, without chance of recovering them in way.
any or idea appreciated.
seems ios having this same issue cookies stored on nshttpcookiestorage.
i've created radar feel free add more issue pushed bit in queue. radar number 16237165.
in meanwhile, can perform following workarounds:
handle cookies manually (aka not rely on ios automatically managing cookies , storing them in
nshttpcookiestorage
class). don't usensuserdefaults
because having same issue.prevent network activity before phone has been unlocked @ least once. option took , did following:
- create file in disk
nsfileprotectioncompleteuntilfirstuserauthentication
permission. - before making network call, check if app able read file. if is, means phone unlocked @ least once, call without loosing cookies.
- create file in disk
here code sample:
static bool phoneisunlocked = no;
//if var true, avoid re checking if file has permissions (cause if granted permissions once, have access now) if(phoneisunlocked) return phoneisunlocked; //if phone has never been unlocked, prevent networking stuff make sure cookies not lost due ios7 bug. //creates file nsfileprotectioncompleteuntilfirstuserauthentication permissions. if app able read it, means phone unlocked @ least once after reboot. //get file path nsstring *documentsdirectory = [nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0]; nsstring *filename = [documentsdirectory stringbyappendingpathcomponent:@"a.secure"]; //create file if doesn't exist if(![[nsfilemanager defaultmanager] fileexistsatpath:filename]) [[nsfilemanager defaultmanager] createfileatpath:filename contents:[@"secure" datausingencoding:nsutf8stringencoding] attributes:[nsdictionary dictionarywithobject:nsfileprotectioncompleteuntilfirstuserauthentication forkey:nsfileprotectionkey]]; nsfilehandle *file = [nsfilehandle filehandleforreadingatpath:filename]; phoneisunlocked = file != nil; //if file not nil, phone has been unlocked [file closefile];
Comments
Post a Comment