Browse Source

[Android] separate openUrl and reloadUrl to keep WebView settings (#187)

* separate openUrl and reloadUrl to keep WebView settings

* enable mixed content support in Android WebView

* enable mixed content support in Android WebView

* increase version number

* always allow mixed content

* reconfig Mixed Content Mode as Compatibility Mode

* fix bug UIViewController nil, support multiple windows in Android

* add global clean cookie function
HungHD 7 years ago
parent
commit
37db748972

+ 43 - 24
android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java

@@ -7,6 +7,9 @@ import android.content.Intent;
 import android.graphics.Point;
 import android.view.Display;
 import android.widget.FrameLayout;
+import android.webkit.CookieManager;
+import android.webkit.ValueCallback;
+import android.os.Build;
 
 import java.util.Map;
 
@@ -70,7 +73,10 @@ public class FlutterWebviewPlugin implements MethodCallHandler, PluginRegistry.A
                 break;
             case "stopLoading":
                 stopLoading(call, result);
-                break;				
+                break;
+            case "cleanCookie":
+                cleanCookie(call, result);
+                break;
             default:
                 result.notImplemented();
                 break;
@@ -86,6 +92,8 @@ public class FlutterWebviewPlugin implements MethodCallHandler, PluginRegistry.A
         boolean clearCookies = call.argument("clearCookies");
         boolean withZoom = call.argument("withZoom");
         boolean withLocalStorage = call.argument("withLocalStorage");
+        boolean supportMultipleWindows = call.argument("supportMultipleWindows");
+        boolean appCacheEnabled = call.argument("appCacheEnabled");
         Map<String, String> headers = call.argument("headers");
         boolean scrollBar = call.argument("scrollBar");
 
@@ -106,7 +114,9 @@ public class FlutterWebviewPlugin implements MethodCallHandler, PluginRegistry.A
                 headers,
                 withZoom,
                 withLocalStorage,
-                scrollBar
+                scrollBar,
+                supportMultipleWindows,
+                appCacheEnabled
         );
         result.success(null);
     }
@@ -132,7 +142,7 @@ public class FlutterWebviewPlugin implements MethodCallHandler, PluginRegistry.A
     }
 
     private void stopLoading(MethodCall call, MethodChannel.Result result) {
-        if (webViewManager != null){
+        if (webViewManager != null) {
             webViewManager.stopLoading(call, result);
         }
     }
@@ -144,47 +154,40 @@ public class FlutterWebviewPlugin implements MethodCallHandler, PluginRegistry.A
         }
     }
 
-    /** 
-    * Navigates back on the Webview.
-    */
+    /**
+     * Navigates back on the Webview.
+     */
     private void back(MethodCall call, MethodChannel.Result result) {
         if (webViewManager != null) {
             webViewManager.back(call, result);
         }
     }
-    /** 
-    * Navigates forward on the Webview.
-    */
+
+    /**
+     * Navigates forward on the Webview.
+     */
     private void forward(MethodCall call, MethodChannel.Result result) {
         if (webViewManager != null) {
             webViewManager.forward(call, result);
         }
     }
 
-    /** 
-    * Reloads the Webview.
-    */
+    /**
+     * Reloads the Webview.
+     */
     private void reload(MethodCall call, MethodChannel.Result result) {
         if (webViewManager != null) {
             webViewManager.reload(call, result);
         }
     }
+
     private void reloadUrl(MethodCall call, MethodChannel.Result result) {
         if (webViewManager != null) {
             String url = call.argument("url");
-            webViewManager.openUrl(false,
-                    false,
-                    false,
-                    false,
-                    "",
-                    url,
-                    null,
-                    false,
-                    false,
-                    false
-            );
+            webViewManager.reloadUrl(url);
         }
     }
+
     private void eval(MethodCall call, final MethodChannel.Result result) {
         if (webViewManager != null) {
             webViewManager.eval(call, result);
@@ -198,17 +201,33 @@ public class FlutterWebviewPlugin implements MethodCallHandler, PluginRegistry.A
         }
         result.success(null);
     }
+
     private void hide(MethodCall call, final MethodChannel.Result result) {
         if (webViewManager != null) {
             webViewManager.hide(call, result);
         }
     }
+
     private void show(MethodCall call, final MethodChannel.Result result) {
         if (webViewManager != null) {
             webViewManager.show(call, result);
         }
     }
 
+    private void cleanCookie(MethodCall call, final MethodChannel.Result result) {
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
+            CookieManager.getInstance().removeAllCookies(new ValueCallback<Boolean>() {
+                @Override
+                public void onReceiveValue(Boolean aBoolean) {
+
+                }
+            });
+        } else {
+            CookieManager.getInstance().removeAllCookie();
+        }
+        result.success(null);
+    }
+
     private int dp2px(Context context, float dp) {
         final float scale = context.getResources().getDisplayMetrics().density;
         return (int) (dp * scale + 0.5f);
@@ -216,7 +235,7 @@ public class FlutterWebviewPlugin implements MethodCallHandler, PluginRegistry.A
 
     @Override
     public boolean onActivityResult(int i, int i1, Intent intent) {
-        if(webViewManager != null && webViewManager.resultHandler != null){
+        if (webViewManager != null && webViewManager.resultHandler != null) {
             return webViewManager.resultHandler.handleResult(i, i1, intent);
         }
         return false;

+ 14 - 1
android/src/main/java/com/flutter_webview_plugin/WebviewManager.java

@@ -12,6 +12,7 @@ import android.view.ViewGroup;
 import android.webkit.CookieManager;
 import android.webkit.ValueCallback;
 import android.webkit.WebChromeClient;
+import android.webkit.WebSettings;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;
 import android.widget.FrameLayout;
@@ -193,11 +194,19 @@ class WebviewManager {
         webView.clearFormData();
     }
 
-    void openUrl(boolean withJavascript, boolean clearCache, boolean hidden, boolean clearCookies, String userAgent, String url, Map<String, String> headers, boolean withZoom, boolean withLocalStorage, boolean scrollBar) {
+    void openUrl(boolean withJavascript, boolean clearCache, boolean hidden, boolean clearCookies, String userAgent, String url, Map<String, String> headers, boolean withZoom, boolean withLocalStorage, boolean scrollBar, boolean supportMultipleWindows, boolean appCacheEnabled) {
         webView.getSettings().setJavaScriptEnabled(withJavascript);
         webView.getSettings().setBuiltInZoomControls(withZoom);
         webView.getSettings().setSupportZoom(withZoom);
         webView.getSettings().setDomStorageEnabled(withLocalStorage);
+        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(supportMultipleWindows);
+        webView.getSettings().setSupportMultipleWindows(supportMultipleWindows);
+        webView.getSettings().setAppCacheEnabled(appCacheEnabled);
+
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
+            Log.d("WebviewManager", "Mixed Content enabled");
+            webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
+        }
 
         if (clearCache) {
             clearCache();
@@ -226,6 +235,10 @@ class WebviewManager {
         }
     }
 
+    void reloadUrl(String url) {
+        webView.loadUrl(url);
+    }
+
     void close(MethodCall call, MethodChannel.Result result) {
         if (webView != null) {
             ViewGroup vg = (ViewGroup) (webView.getParent());

File diff suppressed because it is too large
+ 432 - 314
example/ios/Flutter/flutter_assets/LICENSE


+ 16 - 11
ios/Classes/FlutterWebviewPlugin.m

@@ -58,6 +58,8 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
     } else if ([@"stopLoading" isEqualToString:call.method]) {
         [self stopLoading];
         result(nil);
+    } else if ([@"cleanCookie" isEqualToString:call.method]) {
+        [self cleanCookie];
     } else if ([@"back" isEqualToString:call.method]) {
         [self back];
         result(nil);
@@ -81,27 +83,27 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
     NSString *userAgent = call.arguments[@"userAgent"];
     NSNumber *withZoom = call.arguments[@"withZoom"];
     NSNumber *scrollBar = call.arguments[@"scrollBar"];
-    
+
     if (clearCache != (id)[NSNull null] && [clearCache boolValue]) {
         [[NSURLCache sharedURLCache] removeAllCachedResponses];
     }
-    
+
     if (clearCookies != (id)[NSNull null] && [clearCookies boolValue]) {
         [[NSURLSession sharedSession] resetWithCompletionHandler:^{
         }];
     }
-    
+
     if (userAgent != (id)[NSNull null]) {
         [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent": userAgent}];
     }
-    
+
     CGRect rc;
-    if (rect != nil) {
+    if (rect != (id)[NSNull null]) {
         rc = [self parseRect:rect];
     } else {
         rc = self.viewController.view.bounds;
     }
-    
+
     self.webview = [[WKWebView alloc] initWithFrame:rc];
     self.webview.navigationDelegate = self;
     self.webview.scrollView.delegate = self;
@@ -109,8 +111,6 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
     self.webview.scrollView.showsHorizontalScrollIndicator = [scrollBar boolValue];
     self.webview.scrollView.showsVerticalScrollIndicator = [scrollBar boolValue];
 
-
-
     _enableZoom = [withZoom boolValue];
 
     [self.viewController.view addSubview:self.webview];
@@ -147,11 +147,11 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
             } else {
                 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
                 NSDictionary *headers = call.arguments[@"headers"];
-                
+
                 if (headers != nil) {
                     [request setAllHTTPHeaderFields:headers];
                 }
-                
+
                 [self.webview loadRequest:request];
             }
         }
@@ -229,6 +229,11 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
     }
 }
 
+- (void)cleanCookie {
+    [[NSURLSession sharedSession] resetWithCompletionHandler:^{
+        }];
+}
+
 #pragma mark -- WkWebView Delegate
 - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
     decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
@@ -287,4 +292,4 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
     }
 }
 
-@end
+@end

+ 9 - 2
lib/src/base.dart

@@ -107,7 +107,9 @@ class FlutterWebviewPlugin {
       bool withZoom,
       bool withLocalStorage,
       bool withLocalUrl,
-      bool scrollBar}) async {
+      bool scrollBar,
+      bool supportMultipleWindows,
+      bool appCacheEnabled}) async {
     final args = <String, dynamic>{
       'url': url,
       'withJavascript': withJavascript ?? true,
@@ -119,7 +121,9 @@ class FlutterWebviewPlugin {
       'withZoom': withZoom ?? false,
       'withLocalStorage': withLocalStorage ?? true,
       'withLocalUrl': withLocalUrl ?? false,
-      'scrollBar': scrollBar ?? true
+      'scrollBar': scrollBar ?? true,
+      'supportMultipleWindows': supportMultipleWindows ?? false,
+      'appCacheEnabled': appCacheEnabled ?? false
     };
 
     if (headers != null) {
@@ -168,6 +172,9 @@ class FlutterWebviewPlugin {
     await _channel.invokeMethod('reloadUrl', args);
   }
 
+  // Clean cookie on WebView
+  Future cleanCookie() async => _channel.invokeMethod('cleanCookie');
+
   // Stops current loading process
   Future stopLoading() => _channel.invokeMethod("stopLoading");
 

+ 6 - 0
lib/src/webview_scaffold.dart

@@ -10,6 +10,8 @@ class WebviewScaffold extends StatefulWidget {
   final PreferredSizeWidget appBar;
   final String url;
   final bool withJavascript;
+  final bool supportMultipleWindows;
+  final bool appCacheEnabled;
   final bool clearCache;
   final bool clearCookies;
   final bool enableAppScheme;
@@ -32,6 +34,8 @@ class WebviewScaffold extends StatefulWidget {
       @required this.url,
       this.headers,
       this.withJavascript,
+      this.supportMultipleWindows,
+      this.appCacheEnabled,
       this.clearCache,
       this.clearCookies,
       this.enableAppScheme,
@@ -105,6 +109,8 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
               withLocalStorage: widget.withLocalStorage,
               withLocalUrl: widget.withLocalUrl,
               scrollBar: widget.scrollBar,
+              supportMultipleWindows: widget.supportMultipleWindows,
+              appCacheEnabled: widget.appCacheEnabled,
             );
           } else {
             if (_rect != value) {

Some files were not shown because too many files changed in this diff