question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

iOS cookies: Regardless of 'sharedCookiesEnabled' value, NS/WK cookies (unreliably) synced

See original GitHub issue

Bug description: Syncing of cookies on iOS between ‘React Native side’ (NSHTTPCookieStorage) and WKWebView seems not to follow the documented sharedCookiesEnabled behavior on a real device. I’m observing some type of delayed ‘auto-sync’ between the two sides and sharedCookiesEnabled prop value doesn’t really have any effect. I’m seeing the ‘auto-updating’ happening only on a real device, not on simulator, and there seems to be some unpredictability in the behavior.

I initially observed the behavior with an app having cookies set both by Fetch requests responses and by a website inside react-native-webview WKWebKit. To make a self-contained demo, I put together a simple demo using just react-native-cookies and local react-native-webview. When you set a cookie either for ‘React Native side’ (NSHTTPCookieStorage) or WKWebView side, the other side often (but not always) updates with delay. Whether sharedCookiesEnabled is true or false doesn’t seem to have an effect on this.

Screenshots/Videos:

Screen recordings

Setting NS cookie with ‘sharedCookiesEnabled’ being either undefined or true: WK sometimes updating with a delay:

nsSet

Setting WK cookie: The same happening with NS cookie

wkSet

To Reproduce: Repository: https://github.com/sampok/cookiesync

See App.js in the project:

import React, {useState, useCallback} from 'react';
import {StyleSheet, View, Text, Button} from 'react-native';
import WebView from 'react-native-webview';
import CookieManager from '@react-native-community/cookies';

const url = 'https://test.com';
const name = 'test';

const App = () => {
  const [nsCookieValues, setNsCookieValues] = useState([]);
  const [wkCookieValues, setWkCookieValues] = useState([]);

  const clearCookie = useCallback(async (useWebKit) => {
    setNsCookieValues([]);
    setWkCookieValues([]);
    CookieManager.clearByName(url, name, useWebKit);
  }, []);

  const setCookie = useCallback(async (useWebKit) => {
    CookieManager.set(
      url,
      {name, value: 'set', expires: '2030-01-01T12:00:00.00Z'},
      useWebKit,
    );
    let i = 0;
    while (i++ < 30) {
      CookieManager.get(url).then((cookies) =>
        setNsCookieValues((prev) => [...prev, cookies[name]?.value]),
      );
      CookieManager.get(url, true).then((cookies) =>
        setWkCookieValues((prev) => [...prev, cookies[name]?.value]),
      );
      await new Promise((resolve) => setTimeout(resolve, 300));
    }
  }, []);

  return (
    <View>
      <View style={styles.container}>
        <WebView
          originWhitelist={['*']}
          source={{html: '<p style="background: #eee; height: 100%;">Page</p>'}}
          // sharedCookiesEnabled={true} // 'syncs' cookies with or without this
        />
      </View>

      <View style={styles.horizontal}>
        <View style={styles.half}>
          <Button title="Clear NS Cookie" onPress={() => clearCookie(false)} />
        </View>
        <View style={styles.half}>
          <Button title="Clear WK Cookie" onPress={() => clearCookie(true)} />
        </View>
      </View>

      <View style={styles.horizontal}>
        <View style={styles.half}>
          <Button title="Set NS Cookie" onPress={() => setCookie(false)} />
        </View>
        <View style={styles.half}>
          <Button title="Set WK Cookie" onPress={() => setCookie(true)} />
        </View>
      </View>

      <View style={styles.horizontal}>
        <View style={styles.half}>
          {nsCookieValues.map((value, i) => (
            <Text key={i}>{value ?? '-'}</Text>
          ))}
        </View>

        <View style={styles.half}>
          {wkCookieValues.map((value, i) => (
            <Text key={i}>{value ?? '-'}</Text>
          ))}
        </View>
      </View>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {height: 200, backgroundColor: '#eeeeee'},
  horizontal: {flexDirection: 'row'},
  half: {width: '50%', alignItems: 'center'},
});

export default App;

Expected behavior: Cookie behavior should be reliable, and it should be possible to configure the cookie to be shared (or not shared) using the sharedCookiesEnabled prop.

Environment:

  • iOS SDKs 14.2 and 13.7 tested, on iOS 14.2 and 13.7 iPhones
  • react-native version: 0.63.4
  • react-native-webview version: 11.0.2
  • react-native-cookies version: 5.0.1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:11
  • Comments:12

github_iconTop GitHub Comments

2reactions
srmaguracommented, Jul 5, 2022

This issue should be reopened. Seems to happen on both Android and iOS.

Possibly relevant: Android has CookieManager.flush() method which writes cookies to persistent storage.

1reaction
Kamill90commented, Jan 23, 2022

Any news on this? Ios cookies are still not synced.

Read more comments on GitHub >

github_iconTop Results From Across the Web

iOS 11.3: WKWebView cookie synchronizing stopped working
Working on an app with web content, we switched fro UIWebView to WKWebView after iOS 11. We use URLSession for authentication, then webviews...
Read more >
React Native webview cookies issue - Stack Overflow
I have a issue in webview not being able to set cookies on iOS. Am implementing third party payment gateway ...
Read more >
Sync Cookies Across Webviews and Native App for iOS and ...
Here I provide a view of how we can pass cookies for both of them on iOS and Android, and across different scenarios....
Read more >
Enable 3rd Party Cookies on iPhone iOS Safari - Dacast
Learn how to enable 3rd party cookies on the iPhone and other Apple devices in order to avoid any hiccups with low latency...
Read more >
Understanding & Reacting to Apple's Safari Browser Tracking ...
With its most recent Safari browser release for iOS and MacOS, ... Safari will now block sending cookies to third parties determined to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found