【问题标题】:How can get cookie which is set in webview in react-native?如何在 react-native 中获取在 webview 中设置的 cookie?
【发布时间】:2019-10-22 01:55:02
【问题描述】:

我正在尝试检索用户输入的邮政编码,该邮政编码已添加到 web 视图中的 cookie 中。我如何得到它? 我试过 react-native-cookies 得到一个空对象。

import CookieManager from 'react-native-cookies';

componentWillMount() {
    CookieManager.get('https://www.example.com')
    .then((res) => {
      console.log('CookieManager.get from webkit-view =>', res);
    });
  }
}

<WebView
  style={styles.webview}
  source={{ uri: 'https://www.example.com' }}
  injectedJavaScript={INJECTED_JAVASCRIPT}
  mixedContentMode="compatibility"
  javaScriptEnabled
  domStorageEnabled
  thirdPartyCookiesEnabled
  sharedCookiesEnabled
  originWhitelist={['*']}
  useWebKit
/>

我正在 iOS 模拟器中试用。安卓很好用。 我看到 https://github.com/react-native-community/react-native-webview/pull/175 已合并,但不知道如何使用它来获取用户设置的 cookie。

【问题讨论】:

  • 您使用的是react-native-webview 版本>= 5.7.0 吗?
  • 我使用的是最新版本。 “react-native-cookies”:“3.3.0”,“react-native-webview”:“^5.11.0”

标签: react-native react-native-ios react-native-webview react-native-cookies


【解决方案1】:

您可以在不使用任何本机模块的情况下尝试以下技巧。只需通过代码或 URL。

// @flow

import React, { Component } from 'react';
import {
  WebView,
} from 'react-native';

class LoginScreen extends Component {
  state = {
    cookies    : {},
    webViewUrl : ''
  }

  onNavigationStateChange = (webViewState: { url: string }) => {
    const { url } = webViewState;

    // when WebView.onMessage called, there is not-http(s) url
    if(url.includes('http')) {
      this.setState({ webViewUrl: url })
    }
  }

  _checkNeededCookies = () => {
    const { cookies, webViewUrl } = this.state;

    if (webViewUrl === 'SUCCESS_URL') {
      if (cookies['cookie-name-for-jwt']) {
        alert(cookies['cookie-name-for-jwt']);
        // do your magic...
      }
    }
  }

  _onMessage = (event) => {
    const { data } = event.nativeEvent;
    const cookies  = data.split(';'); // `csrftoken=...; rur=...; mid=...; somethingelse=...`

    cookies.forEach((cookie) => {
      const c = cookie.trim().split('=');

      const new_cookies = this.state.cookies;
      new_cookies[c[0]] = c[1];

      this.setState({ cookies: new_cookies });
    });

    this._checkNeededCookies();
  }

  render() {
    const jsCode = "window.postMessage(document.cookie)"
    // let jsCode = "window.postMessage(document.cookie= 'login=; expires=Bla bla bla')"; // if you need to write some cookies, not sure if it goes to shared cookies, most probably no :)

    return (
      <WebView
        source={{ uri: 'AUTH_URL' }}
        onNavigationStateChange={this.onNavigationStateChange}
        onMessage={this._onMessage}
        injectedJavaScript={jsCode}
        style={{ flex: 1 }}
      />
    );
  }
}

export default LoginScreen;

https://gist.github.com/kanzitelli/e5d198e96f81b7a8263745043766127c

希望这对你有用。

【讨论】:

  • 虽然我想用react-native-cookies 解决这个问题,但它确实有效。所以我接受答案。非常感谢:)
猜你喜欢
  • 1970-01-01
  • 2021-11-26
  • 1970-01-01
  • 1970-01-01
  • 2018-08-22
  • 2018-05-30
  • 2020-07-11
  • 2021-08-09
  • 2022-01-06
相关资源
最近更新 更多