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?
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?
Solved by using this: https://github.com/react-native-community/react-native-cookies
CookieManager.clearAll(); 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 }} /> 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(() => { }) This is the solution without using any third party libraries.
var RCTNetworking = require(“RCTNetworking”); RCTNetworking.clearCookies(() => {}); 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> ); }