Cookie persistence in iOS Safari/Chrome
Asked Answered
K

1

7

My persistent cookies are being deleted when I close and reopen the browser on iOS Safari (and Chrome). I'm on iOS 11, but have tested on iOS10/9 also. The cookies persist correctly on Android and desktop. And strangely, it works fine for Firefox on iOS.

What am i missing here?

Here is my javascript code the sets the cookie and expire date:

  setCookie = function(cname, cvalue, exdays) {
     var d = new Date();
     d.setTime(d.getTime() + (exdays*24*60*60*1000));
     var expires = "expires="+ d.toUTCString();
     document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
  }

  setCookie("test", "random test value", 365);
Karli answered 16/10, 2017 at 16:10 Comment(0)
B
6

I've just come across this problem with cookies being persistent on Android / Desktop devices but not on iOS11 when tested on a production server. The solution seemed to be defining the domain of the cookie:

  setCookie = function(cname, cvalue, exdays) {
     var d = new Date();
     d.setTime(d.getTime() + (exdays*24*60*60*1000));
     var expires = "expires="+ d.toUTCString();
     document.cookie = cname + "=" + cvalue + ";" + expires + ";domain=" + window.location.hostname + ";path=/";
  }

  setCookie("test", "random test value", 365);

iOS11 seems to be much more locked down in terms of what cookies it accepts. I can find lots of marketing blurb about it being better for privacy but very little technical detail about how to implement things (e.g. persistent login / SSO) properly in light of the new restrictions. Can anyone recommend any useful links?

Balaam answered 25/10, 2017 at 21:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.