Browse Source

Compile fix ios (#603)

* Fixed ios compilation, added clear cache for ios

* added cleaning cache for android

* updated readme
Rafal Wachol 6 years ago
parent
commit
fee1fee5a4

+ 4 - 0
CHANGELOG.md

@@ -1,3 +1,7 @@
+# 0.3.9+1
+
+- Fixed error methods on iOS
+
 # 0.3.9
 
 - Fixed error methods on iOS

+ 10 - 0
android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java

@@ -6,6 +6,7 @@ import android.content.Context;
 import android.content.Intent;
 import android.graphics.Point;
 import android.view.Display;
+import android.webkit.WebStorage;
 import android.widget.FrameLayout;
 import android.webkit.CookieManager;
 import android.webkit.ValueCallback;
@@ -90,12 +91,21 @@ public class FlutterWebviewPlugin implements MethodCallHandler, PluginRegistry.A
             case "canGoForward":
                 canGoForward(result);
                 break;
+            case "cleanCache":
+                cleanCache(result);
+                break;
             default:
                 result.notImplemented();
                 break;
         }
     }
 
+    private void cleanCache(MethodChannel.Result result) {
+        webViewManager.cleanCache();
+        WebStorage.getInstance().deleteAllData();
+        result.success(null);
+    }
+
     void openUrl(MethodCall call, MethodChannel.Result result) {
         boolean hidden = call.argument("hidden");
         String url = call.argument("url");

+ 7 - 0
android/src/main/java/com/flutter_webview_plugin/WebviewManager.java

@@ -509,6 +509,13 @@ class WebviewManager {
         return webView.canGoForward();
     }
 
+    /**
+     * Clears cache
+     */
+    void cleanCache(){
+        webView.clearCache(true);
+    }
+
     void hide(MethodCall call, MethodChannel.Result result) {
         if (webView != null) {
             webView.setVisibility(View.GONE);

+ 55 - 16
ios/Classes/FlutterWebviewPlugin.m

@@ -1,5 +1,5 @@
 #import "FlutterWebviewPlugin.h"
-#import "JavaScriptChannelHandler.h"
+#import "WebviewJavaScriptChannelHandler.h"
 
 static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
 
@@ -20,7 +20,7 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
 
     UIViewController *viewController = [UIApplication sharedApplication].delegate.window.rootViewController;
     FlutterWebviewPlugin* instance = [[FlutterWebviewPlugin alloc] initWithViewController:viewController];
-
+    
     [registrar addMethodCallDelegate:instance channel:channel];
 }
 
@@ -35,7 +35,7 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
 - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
     if ([@"launch" isEqualToString:call.method]) {
         if (!self.webview)
-            [self initWebview:call];
+            [self initWebview:call withResult:result];
         else
             [self navigate:call];
         result(nil);
@@ -62,8 +62,7 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
         [self stopLoading];
         result(nil);
     } else if ([@"cleanCookies" isEqualToString:call.method]) {
-        [self cleanCookies];
-        result(nil);
+        [self cleanCookies:result];
     } else if ([@"back" isEqualToString:call.method]) {
         [self back];
         result(nil);
@@ -77,12 +76,14 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
         [self onCanGoBack:call result:result];
     } else if ([@"canGoForward" isEqualToString:call.method]) {
         [self onCanGoForward:call result:result];
+    } else if ([@"cleanCache" isEqualToString:call.method]) {
+        [self cleanCache:result];
     } else {
         result(FlutterMethodNotImplemented);
     }
 }
 
-- (void)initWebview:(FlutterMethodCall*)call {
+- (void)initWebview:(FlutterMethodCall*)call withResult:(FlutterResult)result {
     NSNumber *clearCache = call.arguments[@"clearCache"];
     NSNumber *clearCookies = call.arguments[@"clearCookies"];
     NSNumber *hidden = call.arguments[@"hidden"];
@@ -105,6 +106,8 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
 
     if (clearCache != (id)[NSNull null] && [clearCache boolValue]) {
         [[NSURLCache sharedURLCache] removeAllCachedResponses];
+        [self cleanCache:result];
+
     }
 
     if (clearCookies != (id)[NSNull null] && [clearCookies boolValue]) {
@@ -113,6 +116,9 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
         {
             [storage deleteCookie:cookie];
         }
+
+        [self cleanCookies:result];
+
     }
 
     if (userAgent != (id)[NSNull null]) {
@@ -256,12 +262,45 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
     }
 }
 
-- (void)cleanCookies {
+- (void)cleanCookies:(FlutterResult)result {
     if(self.webview != nil) {
-        NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
-        for (NSHTTPCookie *cookie in [storage cookies])
-        {
-            [storage deleteCookie:cookie];
+        [[NSURLSession sharedSession] resetWithCompletionHandler:^{
+        }];
+        if (@available(iOS 9.0, *)) {
+          NSSet<NSString *> *websiteDataTypes = [NSSet setWithObject:WKWebsiteDataTypeCookies];
+          WKWebsiteDataStore *dataStore = [WKWebsiteDataStore defaultDataStore];
+
+          void (^deleteAndNotify)(NSArray<WKWebsiteDataRecord *> *) =
+              ^(NSArray<WKWebsiteDataRecord *> *cookies) {
+                [dataStore removeDataOfTypes:websiteDataTypes
+                              forDataRecords:cookies
+                           completionHandler:^{
+                            result(nil);
+                           }];
+              };
+
+          [dataStore fetchDataRecordsOfTypes:websiteDataTypes completionHandler:deleteAndNotify];
+        } else {
+          // support for iOS8 tracked in https://github.com/flutter/flutter/issues/27624.
+          NSLog(@"Clearing cookies is not supported for Flutter WebViews prior to iOS 9.");
+        }
+    }
+}
+
+- (void)cleanCache:(FlutterResult)result {
+    if (self.webview != nil) {
+       if (@available(iOS 9.0, *)) {
+          NSSet* cacheDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
+          WKWebsiteDataStore* dataStore = [WKWebsiteDataStore defaultDataStore];
+          NSDate* dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
+          [dataStore removeDataOfTypes:cacheDataTypes
+                         modifiedSince:dateFrom
+                     completionHandler:^{
+              result(nil);
+                     }];
+        } else {
+          // support for iOS8 tracked in https://github.com/flutter/flutter/issues/27624.
+          NSLog(@"Clearing cache is not supported for Flutter WebViews prior to iOS 9.");
         }
     }
 }
@@ -311,7 +350,7 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
 
 - (bool)checkInvalidUrl:(NSURL*)url {
   NSString* urlString = url != nil ? [url absoluteString] : nil;
-  if (_invalidUrlRegex != [NSNull null] && urlString != nil) {
+  if (![_invalidUrlRegex isEqual:[NSNull null]] && urlString != nil) {
     NSError* error = NULL;
     NSRegularExpression* regex =
         [NSRegularExpression regularExpressionWithPattern:_invalidUrlRegex
@@ -331,10 +370,10 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
     decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
 
     BOOL isInvalid = [self checkInvalidUrl: navigationAction.request.URL];
-
+    
     id data = @{@"url": navigationAction.request.URL.absoluteString,
                 @"type": isInvalid ? @"abortLoad" : @"shouldStart",
-                @"navigationType": [NSNumber numberWithInt:navigationAction.navigationType]};
+                @"navigationType": [NSNumber numberWithInteger:navigationAction.navigationType]};
     [channel invokeMethod:@"onState" arguments:data];
 
     if (navigationAction.navigationType == WKNavigationTypeBackForward) {
@@ -401,8 +440,8 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
 - (void)registerJavaScriptChannels:(NSSet*)channelNames
                         controller:(WKUserContentController*)userContentController {
     for (NSString* channelName in channelNames) {
-        FLTJavaScriptChannel* _channel =
-        [[FLTJavaScriptChannel alloc] initWithMethodChannel: channel
+        FLTCommunityJavaScriptChannel* _channel =
+        [[FLTCommunityJavaScriptChannel alloc] initWithMethodChannel: channel
                                       javaScriptChannelName:channelName];
         [userContentController addScriptMessageHandler:_channel name:channelName];
         NSString* wrapperSource = [NSString

+ 1 - 1
ios/Classes/JavaScriptChannelHandler.h → ios/Classes/WebviewJavaScriptChannelHandler.h

@@ -7,7 +7,7 @@
 
 NS_ASSUME_NONNULL_BEGIN
 
-@interface FLTJavaScriptChannel : NSObject <WKScriptMessageHandler>
+@interface FLTCommunityJavaScriptChannel : NSObject <WKScriptMessageHandler>
 
 - (instancetype)initWithMethodChannel:(FlutterMethodChannel*)methodChannel
                 javaScriptChannelName:(NSString*)javaScriptChannelName;

+ 2 - 2
ios/Classes/JavaScriptChannelHandler.m → ios/Classes/WebviewJavaScriptChannelHandler.m

@@ -2,9 +2,9 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#import "JavaScriptChannelHandler.h"
+#import "WebviewJavaScriptChannelHandler.h"
 
-@implementation FLTJavaScriptChannel {
+@implementation FLTCommunityJavaScriptChannel {
   FlutterMethodChannel* _methodChannel;
   NSString* _javaScriptChannelName;
 }

+ 3 - 0
lib/src/base.dart

@@ -254,6 +254,9 @@ class FlutterWebviewPlugin {
   // Shows the webview
   Future<Null> show() async => await _channel.invokeMethod('show');
 
+  // Clears browser cache
+  Future<Null> clearCache() async => await _channel.invokeMethod('cleanCache');
+
   // Reload webview with a url
   Future<Null> reloadUrl(String url, {Map<String, String> headers}) async {
     final args = <String, dynamic>{'url': url};

+ 1 - 1
pubspec.yaml

@@ -7,7 +7,7 @@ authors:
 - Simon Lightfoot <simon@devangels.london>
 - Rafal Wachol <gorudonu@gmail.com>
 homepage: https://github.com/dart-flitter/flutter_webview_plugin
-version: 0.3.9
+version: 0.3.9+1
 maintainer: Rafal Wachol (@RafalWachol) 
 
 environment: