Problem generation phenomenon

Webview_flutter_webview_plugin: ^0.3.10+1 webview_cookie_manager: ^1.0.0Copy the code

The WebView cookie control in Flutter works fine on Android but poorly on ios. Especially if ios is using WKWebView, setcookies on Flutter do not work for the first time. The process must be killed before a second boot takes effect. And then there are probabilistic set cookies that are not valid. This is really embarrassing, when you successfully log in to Native your H5 does not get your login status.

The solution

If cross-platform solutions don’t work, then of course you have to find a way to make up for it on the original console by injecting your cookies directly on ios

 NSMutableDictionary *cookieProperties1 = [NSMutableDictionary dictionary];
        [cookieProperties1 setObject:@"cookie-key" forKey:NSHTTPCookieName];
        [cookieProperties1 setObject:@"domain" forKey:NSHTTPCookieDomain];
        [cookieProperties1 setObject:@"/" forKey:NSHTTPCookiePath];
        NSHTTPCookie *cookie1 = [NSHTTPCookie cookieWithProperties:cookieProperties1];
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie1];

Copy the code

This step is very simple in ios with oc code set cookies, the last key step is below

Find this file in your project directory, which is actually part of the OC code that Flutter wrote for you.

Find the initWebview method and add the code in the red box: