Skip to content

fixed cleaning cookies for webview #597

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions ios/Classes/FlutterWebviewPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
[self stopLoading];
result(nil);
} else if ([@"cleanCookies" isEqualToString:call.method]) {
[[NSURLSession sharedSession] resetWithCompletionHandler:^{
result(nil);
}];
[self cleanCookies];
result(nil);
} else if ([@"back" isEqualToString:call.method]) {
[self back];
result(nil);
Expand Down Expand Up @@ -105,17 +104,10 @@ - (void)initWebview:(FlutterMethodCall*)call {
}

if (clearCookies != (id)[NSNull null] && [clearCookies boolValue]) {
if (@available(iOS 9.0, *)) {
NSSet *websiteDataTypes
= [NSSet setWithArray:@[
WKWebsiteDataTypeCookies,
]];
NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];

[[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
}];
} else {
// Fallback on earlier versions
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [storage cookies])
{
[storage deleteCookie:cookie];
}
}

Expand Down Expand Up @@ -259,6 +251,17 @@ - (void)reloadUrl:(FlutterMethodCall*)call {
[self.webview loadRequest:request];
}
}

- (void)cleanCookies {
if(self.webview != nil) {
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [storage cookies])
{
[storage deleteCookie:cookie];
}
}
}

- (void)show {
if (self.webview != nil) {
self.webview.hidden = false;
Expand Down
7 changes: 5 additions & 2 deletions lib/src/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,11 @@ class FlutterWebviewPlugin {
}

// Clean cookies on WebView
Future<Null> cleanCookies() async =>
await _channel.invokeMethod('cleanCookies');
Future<Null> cleanCookies() async {
// one liner to clear javascript cookies
await evalJavascript('document.cookie.split(";").forEach(function(c) { document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/"); });');
return await _channel.invokeMethod('cleanCookies');
}

// Stops current loading process
Future<Null> stopLoading() async =>
Expand Down