gpt4 book ai didi

react-native - 如何从 iOS 上的 react native webView 获取 cookie?

转载 作者:行者123 更新时间:2023-12-04 16:39:49 31 4
gpt4 key购买 nike

我在 webView 中打开了一个单点登录站点。用户登录后,我需要使用 cookie 来从 API 请求 token ,并在应用程序中使用它来处理所有其他请求。
这在 Android 上一切正常,但在 iOS 上,当我请求 token 时没有 cookie。
WebView 配置为使用共享 cookie 存储:

    <WebView
source={{ uri: loginUrl }}
onMessage={handleSsoLogin}
injectedJavaScriptBeforeContentLoaded={JS_INTERFACE}
sharedCookiesEnabled={true}
/>
如果我使用 devTools 检查 webView,我可以看到 cookie。另外,如果我再次访问 SSO 站点,我已经记录了它。因此,cookie 由 webView 持久化和使用。 fetch 不使用它们并且 CookieManager 不访问它们:
const cookies = await CookieManager.get(url);
console.log({ cookies });
...
{"cookies": {}}
所以,问题是,我怎样才能从 webView 中获取这些 cookie?

最佳答案

您需要使用 import CookieManager from "@react-native-community/cookies";
在 webview 中,你可以这样做:

 <WebView
cacheEnabled={false}
ref={ref => (this.webView = ref)}
source={{
uri: "http://yoururl.com"
}}
style={{ marginTop: 20 }}
onNavigationStateChange={this.navChange}
thirdPartyCookiesEnabled={true}
/>
{this.state.loading && (
<ActivityIndicator
color={"blue"}
style={{ flex: 20 }}
size={"large"}
/>


navChange = e => {
console.log("e", e);
this.setState({ loading: e.loading });
if (e.url == "http://yoururl.com/landing") {
CookieManager.getAll(true).then(res => {
console.log("CookieManager.getAll =>", res);
if (!!res) {
console.log({res})
CookieManager.clearAll(true).then(res => {
console.log("LoginScreen CookieManager.clearAll =>", res);
});
}
});
}
};

关于react-native - 如何从 iOS 上的 react native webView 获取 cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63168705/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com