Quellcode durchsuchen

Allow using "file:///" urls in Android. (#122)

* added allowFileURLs for Android
Dev Aggarwal vor 7 Jahren
Ursprung
Commit
435ef577ad

+ 55 - 43
README.md

@@ -1,11 +1,10 @@
-[![pub package](https://img.shields.io/pub/v/flutter_webview_plugin.svg)](https://pub.dartlang.org/packages/flutter_webview_plugin) 
-
+[![pub package](https://img.shields.io/pub/v/flutter_webview_plugin.svg)](https://pub.dartlang.org/packages/flutter_webview_plugin)
 
 # flutter_webview_plugin
 
 Plugin that allows Flutter to communicate with a native WebView.
 
-***Warning:***
+**_Warning:_**
 The webview is not integrated in the widget tree, it is a native view on top of the flutter view.
 you won't be able to use snackbars, dialogs ...
 
@@ -21,11 +20,11 @@ For help getting started with Flutter, view our online [documentation](http://fl
 new MaterialApp(
       routes: {
         "/": (_) => new WebviewScaffold(
-              url: "https://www.google.com",
-              appBar: new AppBar(
-                title: new Text("Widget webview"),
-              ),
-            )
+          url: "https://www.google.com",
+          appBar: new AppBar(
+            title: new Text("Widget webview"),
+          ),
+        ),
       },
     );
 ```
@@ -44,20 +43,20 @@ return new MaterialApp(
   routes: {
     '/': (_) => const MyHomePage(title: 'Flutter WebView Demo'),
     '/widget': (_) => new WebviewScaffold(
-          url: selectedUrl,
-          appBar: new AppBar(
-            title: const Text('Widget webview'),
-          ),
-          withZoom: true,
-          withLocalStorage: true,
-          hidden: true,
-          initialChild: Container(
-            color: Colors.redAccent,
-            child: const Center(
-              child: Text('Waiting.....'),
-            ),
-          ),
-        )
+      url: selectedUrl,
+      appBar: new AppBar(
+        title: const Text('Widget webview'),
+      ),
+      withZoom: true,
+      withLocalStorage: true,
+      hidden: true,
+      initialChild: Container(
+        color: Colors.redAccent,
+        child: const Center(
+          child: Text('Waiting.....'),
+        ),
+      ),
+    ),
   },
 );
 ```
@@ -66,11 +65,12 @@ return new MaterialApp(
 so you can take control of the webview from anywhere in the app
 
 listen for events
+
 ```dart
 final flutterWebviewPlugin = new FlutterWebviewPlugin();
 
 flutterWebviewPlugin.onUrlChanged.listen((String url) {
-  
+
 });
 ```
 
@@ -111,12 +111,14 @@ flutterWebviewPlugin.close();
 final flutterWebviewPlugin = new FlutterWebviewPlugin();  
 
 flutterWebviewPlugin.launch(url,
-                  fullScreen: false,
-                  rect: new Rect.fromLTWH(
-                      0.0, 
-                      0.0, 
-                      MediaQuery.of(context).size.width, 
-                      300.0));
+  fullScreen: false,
+  rect: new Rect.fromLTWH(
+    0.0,
+    0.0,
+    MediaQuery.of(context).size.width,
+    300.0,
+  ),
+);
 ```
 
 ### Webview Events
@@ -126,41 +128,51 @@ flutterWebviewPlugin.launch(url,
 - `Stream<WebViewStateChanged>` onStateChanged
 - `Stream<String>` onError
 
-***Don't forget to dispose webview***
+**_Don't forget to dispose webview_**
 `flutterWebviewPlugin.dispose()`
 
 ### Webview Functions
 
 ```dart
-Future<Null> launch(String url,
-         {Map<String, String> headers: null,
-         bool withJavascript: true,
-         bool clearCache: false,
-         bool clearCookies: false,
-         bool hidden: false,
-         bool enableAppScheme: true,
-         Rect rect: null,
-         String userAgent: null,
-         bool withZoom: false,
-         bool withLocalStorage: true,
-         bool withLocalUrl: true,
-         bool scrollBar: true});
+Future<Null> launch(String url, {
+   Map<String, String> headers: null,
+   bool withJavascript: true,
+   bool clearCache: false,
+   bool clearCookies: false,
+   bool hidden: false,
+   bool enableAppScheme: true,
+   Rect rect: null,
+   String userAgent: null,
+   bool withZoom: false,
+   bool withLocalStorage: true,
+   bool withLocalUrl: true,
+   bool scrollBar: true,
+   bool supportMultipleWindows: false,
+   bool appCacheEnabled: false,
+   bool allowFileURLs: false,
+});
 ```
+
 ```dart
 Future<String> evalJavascript(String code);
 ```
+
 ```dart
 Future<Map<String, dynamic>> getCookies();
 ```
+
 ```dart
 Future<Null> resize(Rect rect);
 ```
+
 ```dart
 Future<Null> show();
 ```
+
 ```dart
 Future<Null> hide();
 ```
+
 ```dart
 Future<Null> reloadUrl(String url);
 ```

+ 3 - 1
android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java

@@ -96,6 +96,7 @@ public class FlutterWebviewPlugin implements MethodCallHandler, PluginRegistry.A
         boolean appCacheEnabled = call.argument("appCacheEnabled");
         Map<String, String> headers = call.argument("headers");
         boolean scrollBar = call.argument("scrollBar");
+        boolean allowFileURLs = call.argument("allowFileURLs");
 
         if (webViewManager == null || webViewManager.closed == true) {
             webViewManager = new WebviewManager(activity);
@@ -116,7 +117,8 @@ public class FlutterWebviewPlugin implements MethodCallHandler, PluginRegistry.A
                 withLocalStorage,
                 scrollBar,
                 supportMultipleWindows,
-                appCacheEnabled
+                appCacheEnabled,
+                allowFileURLs
         );
         result.success(null);
     }

+ 20 - 2
android/src/main/java/com/flutter_webview_plugin/WebviewManager.java

@@ -194,17 +194,35 @@ 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, boolean supportMultipleWindows, boolean appCacheEnabled) {
+    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, 
+            boolean allowFileURLs, 
+    ) {
         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);
+      
+        webView.getSettings().setAllowFileAccessFromFileURLs(allowFileURLs);
+        webView.getSettings().setAllowUniversalAccessFromFileURLs(allowFileURLs);
 
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
-            Log.d("WebviewManager", "Mixed Content enabled");
             webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
         }
 

+ 6 - 2
lib/src/base.dart

@@ -1,5 +1,6 @@
 import 'dart:async';
 import 'dart:ui';
+import 'dart:io';
 
 import 'package:flutter/material.dart';
 import 'package:flutter/services.dart';
@@ -109,7 +110,9 @@ class FlutterWebviewPlugin {
       bool withLocalUrl,
       bool scrollBar,
       bool supportMultipleWindows,
-      bool appCacheEnabled}) async {
+      bool appCacheEnabled,
+      bool allowFileURLs,
+    }) async {
     final args = <String, dynamic>{
       'url': url,
       'withJavascript': withJavascript ?? true,
@@ -123,7 +126,8 @@ class FlutterWebviewPlugin {
       'withLocalUrl': withLocalUrl ?? false,
       'scrollBar': scrollBar ?? true,
       'supportMultipleWindows': supportMultipleWindows ?? false,
-      'appCacheEnabled': appCacheEnabled ?? false
+      'appCacheEnabled': appCacheEnabled ?? false,
+      "allowFileURLs": allowFileURLs ?? false,
     };
 
     if (headers != null) {

+ 5 - 1
lib/src/webview_scaffold.dart

@@ -22,6 +22,7 @@ class WebviewScaffold extends StatefulWidget {
   final bool withZoom;
   final bool withLocalStorage;
   final bool withLocalUrl;
+  final bool allowFileURLs;
   final bool scrollBar;
   final bool hidden;
   final Widget initialChild;
@@ -48,7 +49,9 @@ class WebviewScaffold extends StatefulWidget {
       this.withLocalUrl,
       this.scrollBar,
       this.hidden = false,
-      this.initialChild})
+      this.initialChild,
+      this.allowFileURLs,
+      })
       : super(key: key);
 
   @override
@@ -111,6 +114,7 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
               scrollBar: widget.scrollBar,
               supportMultipleWindows: widget.supportMultipleWindows,
               appCacheEnabled: widget.appCacheEnabled,
+              allowFileURLs: widget.allowFileURLs,
             );
           } else {
             if (_rect != value) {