0

所以我试图在 Webview 中设置一个 cookie(设置用户令牌),但我遇到了页面不断刷新的问题。这是代码:

InAppWebViewController _webViewController;
  final expiresDate = DateTime.now().add(Duration(days: 3)).millisecondsSinceEpoch;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: DashboardPageTitlesAtom(
        title: widget.title,
        leading: IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
        trailing: IconButton(
          icon: Icon(CupertinoIcons.refresh),
          onPressed: _webViewController == null ? null : () => _webViewController.reload(),
        ),
      ),
   
      body: Column(
        children: [
          Divider(
            thickness: 1,
            height: 1,
          ),
          Expanded(
            child: InAppWebView(
                initialOptions: InAppWebViewGroupOptions(
                  crossPlatform: InAppWebViewOptions(
                    javaScriptEnabled: true,
                    clearCache: false,
                  ),
                ),
                onWebViewCreated: (InAppWebViewController controller) async {
                  _webViewController = controller;
                },
                onLoadStop: (InAppWebViewController controller, url) async {
                  try {
                    final token = await getToken();
                    await _webViewController.evaluateJavascript(
                      source:
                          'document.cookie = "accessToken=${token}"; secure=true; httpOnly=false; sameSite=None',
                    );
                    await _webViewController.loadUrl(url: widget.url);
                  } catch (e) {
                    print(e.toString());
                  }
                }),
          ),
        ],
      ),
    );
  }

问题是设置cookie的唯一方法是在评估javascript之后刷新页面并且页面刷新不会停止。谢谢你。

4

2 回答 2

1

您正在事件中调用该loadUrl方法onLoadStop。这样您就可以创建一个循环:

"onLoadStop" -> "loadUrl" -> "onLoadStop" -> "loadUrl" -> "onLoadStop" -> etc.

onLoadStop被调用时,您正在加载 URL,因此onLoadStop将再次调用该新 URL 请求。

为了实现您的需要,您应该在CookieManager创建 WebView 时(即在onWebViewCreated 事件中)使用该类并设置 cookie。然后,您loadUrl使用您的 URL 请求调用该方法。

在您的场景中,您将拥有:

onWebViewCreated: (controller) async {
  _webViewController = controller;
  try {
    // get the access token
    final token = await getToken();

    // get the CookieManager instance
    CookieManager cookieManager = CookieManager.instance();

    // set the access token
    await cookieManager.setCookie(
      url: widget.url,
      name: "accessToken",
      value: token,
      isSecure: true,
      isHttpOnly: false,
      sameSite: HTTPCookieSameSitePolicy.NONE
    );

    // then load your initial URL here
    await _webViewController.loadUrl(url: widget.url);
  } catch (e) {
    print(e.toString());
  }
},
于 2021-03-24T14:48:50.147 回答
1

试试CookieManager

调用这样的东西:

CookieManager _cookieManager = CookieManager.instance();

final expiresDate =
    DateTime.now().add(Duration(days: 3)).millisecondsSinceEpoch;
`enter code here`_cookieManager.setCookie(
  url: "https://flutter.dev/",
  name: "session",
  value: "54th5hfdcfg34",
  domain: ".flutter.dev",
  expiresDate: expiresDate,
  isSecure: true,
);
于 2021-03-23T10:39:08.513 回答