19

How to clear react native webview cookies?

When I re-open the page, it remembers my account if I logged in on a website. But I don't want that.

Can I do this by injecting javascript?

1
  • 1
    yes you can clear cookies through JavaScript Commented Jun 16, 2017 at 22:21

5 Answers 5

22

Solved by using this: https://github.com/react-native-community/react-native-cookies

CookieManager.clearAll(); 
Sign up to request clarification or add additional context in comments.

3 Comments

This package and its alternatives do not work on RN 0.60
This library does not provide support for RN >= 0.60
Use github.com/react-native-community/react-native-cookies instead. This version works well with RN >= 0.60. I am currently using this package with react-native 0.61.5 without any issues.
18

I used incognito mode as a turnaround and it worked perfect for me.

<WebView incognito={true} userAgent="Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko" source={{ uri: 'your url', }} style={{ flex: 1 }} /> 

2 Comments

Great answer. Works for iOS and Android and does not need any extra dependencies!
Incognito does not fix it for me.
13

At the time I'm posting, CookieManager has poor support for Expo, I had problems clearing cookies after changing a password.

In 2020, for those using Expo, this works:

const RCTNetworking = require('react-native/Libraries/Network/RCTNetworking') RCTNetworking.clearCookies(() => { }) 

Comments

3

This is the solution without using any third party libraries.

var RCTNetworking = require(“RCTNetworking”); RCTNetworking.clearCookies(() => {}); 

3 Comments

You need to have explained this
I am using old Instagram API which requires the cookies to be deleted in order for the user to logout. This solution does not clear the cookies.
yes, this doesn't appear to clear cookies from react-native-linkedin either
0

2024 answer for Expo-users

Couldn't find a single library to do this. Adeb's answer was great for simple, starting off fresh situation, but I needed some custom behavior regarding cookies.

Inject some custom Javascript to manipulate the cookies.

Depending on your setup, use Cookie Store API for certain and newer browsers, or more manual methods of cookie clearing.

 export default function HomeScreen() { const webView = useRef<WebView>(null) const injectCookies = () => { webView.current?.injectJavaScript(` console.log("manipulate your cookies here, for example using cookiestore api"); `) } return ( <View style={styles.container}> <View style={styles.banner}> <Button onPress={injectCookies} title="button"></Button> </View> <WebView source={{uri: localDev}} /> </View> ); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.