5

I am building a web app that uses js-cookie. I use the following to import the js-cookie library into my html/javascript file:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jscookie/2.1.4/js.cookie.js"></script>

My js-cookie code works perfectly with Android devices: Cookies are created and retrieved, just as expected. However, the same code fails to write/read cookies when executed on iOS devices (iPhones). The browser doesn't matter; I have tested with the latest versions of Safari, Chrome, and Firefox for iOS.

The cookies, when written, come back as undefined when I attempt to read them on iOS devices. Here is a snippet of code that I use to write the cookies:

    //Set expiration cookie...
    nowDate = new Date();
    expiresDate = new Date(nowDate.getTime() + (120 * 120000)); 
    //cookie expires in 4 hours
    Cookies.set('expires', expiresDate, { expires: expiresDate });

Any subsequent attempt to read these cookies works perfectly on Android devices, but not on iOS devices. Here's an example piece of code I use to read the cookie:

    expiresCookie = Cookies.get('expires'); //get expiresCookie

What am I doing wrong?

SpeedGolfer
  • 255
  • 3
  • 13
  • Debug mobile safari connecting it to a mac with a real web inspector, that may help. I encountered the same issue and it was a piece of code elsewhere that was ES6 compatible (so it was running ok in all other modern browsers) bu was failing iOS9 which is i believe ES5. The code was a function with default arguments `function a(a=1,b=2) {}` rewriting that solved the iOS9 hiccup. – lavirius May 26 '18 at 15:52

2 Answers2

2

The browser you use on iOs has no impact because, it's always the Safari engine. On iOs, chrome, firefox and others are just frame around the safari webview. That's for the pro tip.

Now, in your problem. Can you access to the classical cookie API ? You can do it using this :

document.cookie

If no, you have a very big problem. Maybe you've already moved to iOS 11 and be included in the no-more-cookie policy : http://adage.com/article/digitalnext/ios-11-kill-cookie/311444/ Or maybe you have enabled an ad-block/do-not-track feature.

MathKimRobin
  • 1,268
  • 3
  • 21
  • 52
  • @SpeedGolfer, can you please verify if this is the issue? – Fagner Brack Dec 28 '17 at 08:50
  • @FagnerBrack: I will let you know if I can confirm this is the issue. At the moment, I'm exploring the use of localStorage as an alternative that could be more friendly to iOS. – SpeedGolfer Jan 05 '18 at 22:54
  • Yes I confirm, it's more friendly and more reliable. And for european visitors, localstorage is not concerned by the big law about cookies and privacy – MathKimRobin Jan 06 '18 at 07:22
1

First, I think you have a trouble with you link to the script. If you did your first tests on android maybe your device still keep this file in your cache.

try with this url for having latest version: https://cdnjs.cloudflare.com/ajax/libs/js-cookie/latest/js.cookie.js

Then, check also that your url is not in file location (file:///.../file.html) instead of http (http://domain/file.html). Browsers doesn't save cookies when there is no domain and no http communication.

Fabien Leborgne
  • 377
  • 1
  • 5