瀏覽代碼

fixed cleaning cookies for webview (#597)

Rafal Wachol 6 年之前
父節點
當前提交
f02023992b
共有 2 個文件被更改,包括 22 次插入16 次删除
  1. 17 14
      ios/Classes/FlutterWebviewPlugin.m
  2. 5 2
      lib/src/base.dart

+ 17 - 14
ios/Classes/FlutterWebviewPlugin.m

@@ -62,9 +62,8 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
         [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);
@@ -105,17 +104,10 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
     }
 
     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];
         }
     }
 
@@ -259,6 +251,17 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
         [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;

+ 5 - 2
lib/src/base.dart

@@ -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 =>