flutter开发实战 - inappwebview设置cookie

flutter开发实战-inappwebview设置cookie

在使用inappwebview时候,需要设置cookie,这里记录一下

一、在initialUserScripts中设置cookie

在inappwebview中有一个initialUserScripts,可以初始化设置cookie等,我们可以通过该属性来设置cookie。

复制代码
initialUserScripts: UnmodifiableListView<UserScript>([
              UserScript(
                  source:
                      "document.cookie='token=${TokenAuth.getToken()};domain='.inice.cn';path=/'",
                  injectionTime: UserScriptInjectionTime.AT_DOCUMENT_START),
            ]),

二、在onWebViewCreated回调设置cookie

在inappWebview设置cookie,可以使用到CookieManager,设置cookie

复制代码
CookieManager.instance().setCookie(
          url: Uri.parse(url),
          name: "token",
          value: TokenAuth.getToken(),
          domain: ".inice.cn",
          isHttpOnly: false,
        );

在onWebViewCreated方法中调用上面的代码

复制代码
onWebViewCreated: (controller) {
    webViewController = controller;
	
	CookieManager.instance().setCookie(
          url: Uri.parse(url),
          name: "token",
          value: TokenAuth.getToken(),
          domain: ".inice.cn",
          isHttpOnly: false,
        );
},

三、小结

flutter开发实战-inappwebview设置cookie。描述可能不是特别准确,请见谅。

学习记录,每天不停进步。

相关推荐
雪铃儿13 小时前
Flutter 双端热更新新方案:基于 Dart 官方动态模块的 flutter_patcher 1.0.0
flutter·hotfix
好的佩奇19 小时前
Dart 之方法、操作符与可调用对象
android·flutter·dart
恋猫de小郭19 小时前
Flutter Impeller Vulkan 内存持续上涨,问题没想到是文字阴影
android·前端·flutter
传奇开心果2 天前
【Flutter踩坑排雷】第3课:const 误用坑|const 错误使用引发 UI 异常与性能损耗
android·flutter
行者-全栈开发2 天前
Flutter 插件 device_calendar_plus 开源鸿蒙适配实战:从权限弹窗失灵到循环日程跑通
flutter·跨平台·harmonyos·arkts·日历·适配·开源鸿蒙
恋猫de小郭2 天前
Flutter 开始支持 Jetpack Compose Platform View
android·前端·flutter
SoaringHeart2 天前
Flutter 查缺补漏:@JsonKey 属性全解,让模型层不再被后端字段牵着走
flutter
恋猫de小郭3 天前
Flutter 可以更直接地调用原生 API 了,Dart Direct Native Interop
android·前端·flutter
码点3 天前
二.Flutter 速成第二课:核心概念与第一个自定义页面
flutter