Przeglądaj źródła

Merge pull request #493 from rafern/master

Fix iOS local URL support (fixes #114)
Rafal Wachol 6 lat temu
rodzic
commit
ca7957dead

+ 5 - 0
README.md

@@ -161,6 +161,10 @@ Future<String> loadJS(String name) async {
 }
 }
 ```
 ```
 
 
+### Accessing local files in the file system
+Set the `withLocalUrl` option to true in the launch function or in the Webview scaffold to enable support for local URLs.
+
+Note that, on iOS, the `localUrlScope` option also needs to be set to a path to a directory. All files inside this folder (or subfolder) will be allowed access. If ommited, only the local file being opened will have access allowed, resulting in no subresources being loaded. This option is ignored on Android.
 
 
 ### Webview Events
 ### Webview Events
 
 
@@ -189,6 +193,7 @@ Future<Null> launch(String url, {
    bool withZoom: false,
    bool withZoom: false,
    bool withLocalStorage: true,
    bool withLocalStorage: true,
    bool withLocalUrl: true,
    bool withLocalUrl: true,
+   String localUrlScope: null,
    bool scrollBar: true,
    bool scrollBar: true,
    bool supportMultipleWindows: false,
    bool supportMultipleWindows: false,
    bool appCacheEnabled: false,
    bool appCacheEnabled: false,

+ 10 - 2
ios/Classes/FlutterWebviewPlugin.m

@@ -164,8 +164,15 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
             NSNumber *withLocalUrl = call.arguments[@"withLocalUrl"];
             NSNumber *withLocalUrl = call.arguments[@"withLocalUrl"];
             if ( [withLocalUrl boolValue]) {
             if ( [withLocalUrl boolValue]) {
                 NSURL *htmlUrl = [NSURL fileURLWithPath:url isDirectory:false];
                 NSURL *htmlUrl = [NSURL fileURLWithPath:url isDirectory:false];
+                NSString *localUrlScope = call.arguments[@"localUrlScope"];
                 if (@available(iOS 9.0, *)) {
                 if (@available(iOS 9.0, *)) {
-                    [self.webview loadFileURL:htmlUrl allowingReadAccessToURL:htmlUrl];
+                    if(localUrlScope == nil) {
+                        [self.webview loadFileURL:htmlUrl allowingReadAccessToURL:htmlUrl];
+                    }
+                    else {
+                        NSURL *scopeUrl = [NSURL fileURLWithPath:localUrlScope];
+                        [self.webview loadFileURL:htmlUrl allowingReadAccessToURL:scopeUrl];
+                    }
                 } else {
                 } else {
                     @throw @"not available on version earlier than ios 9.0";
                     @throw @"not available on version earlier than ios 9.0";
                 }
                 }
@@ -312,7 +319,8 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
     if (_enableAppScheme ||
     if (_enableAppScheme ||
         ([webView.URL.scheme isEqualToString:@"http"] ||
         ([webView.URL.scheme isEqualToString:@"http"] ||
          [webView.URL.scheme isEqualToString:@"https"] ||
          [webView.URL.scheme isEqualToString:@"https"] ||
-         [webView.URL.scheme isEqualToString:@"about"])) {
+         [webView.URL.scheme isEqualToString:@"about"] ||
+         [webView.URL.scheme isEqualToString:@"file"])) {
          if (isInvalid) {
          if (isInvalid) {
             decisionHandler(WKNavigationActionPolicyCancel);
             decisionHandler(WKNavigationActionPolicyCancel);
          } else {
          } else {

+ 6 - 0
lib/src/base.dart

@@ -106,6 +106,10 @@ class FlutterWebviewPlugin {
   ///     It is always enabled in UIWebView of iOS and  can not be disabled.
   ///     It is always enabled in UIWebView of iOS and  can not be disabled.
   /// - [withLocalUrl]: allow url as a local path
   /// - [withLocalUrl]: allow url as a local path
   ///     Allow local files on iOs > 9.0
   ///     Allow local files on iOs > 9.0
+  /// - [localUrlScope]: allowed folder for local paths
+  ///     iOS only.
+  ///     If null and withLocalUrl is true, then it will use the url as the scope,
+  ///     allowing only itself to be read.
   /// - [scrollBar]: enable or disable scrollbar
   /// - [scrollBar]: enable or disable scrollbar
   /// - [supportMultipleWindows] enable multiple windows support in Android
   /// - [supportMultipleWindows] enable multiple windows support in Android
   /// - [invalidUrlRegex] is the regular expression of URLs that web view shouldn't load.
   /// - [invalidUrlRegex] is the regular expression of URLs that web view shouldn't load.
@@ -128,6 +132,7 @@ class FlutterWebviewPlugin {
     bool displayZoomControls,
     bool displayZoomControls,
     bool withLocalStorage,
     bool withLocalStorage,
     bool withLocalUrl,
     bool withLocalUrl,
+    String localUrlScope,
     bool withOverviewMode,
     bool withOverviewMode,
     bool scrollBar,
     bool scrollBar,
     bool supportMultipleWindows,
     bool supportMultipleWindows,
@@ -150,6 +155,7 @@ class FlutterWebviewPlugin {
       'displayZoomControls': displayZoomControls ?? false,
       'displayZoomControls': displayZoomControls ?? false,
       'withLocalStorage': withLocalStorage ?? true,
       'withLocalStorage': withLocalStorage ?? true,
       'withLocalUrl': withLocalUrl ?? false,
       'withLocalUrl': withLocalUrl ?? false,
+      'localUrlScope': localUrlScope,
       'scrollBar': scrollBar ?? true,
       'scrollBar': scrollBar ?? true,
       'supportMultipleWindows': supportMultipleWindows ?? false,
       'supportMultipleWindows': supportMultipleWindows ?? false,
       'appCacheEnabled': appCacheEnabled ?? false,
       'appCacheEnabled': appCacheEnabled ?? false,

+ 3 - 0
lib/src/webview_scaffold.dart

@@ -25,6 +25,7 @@ class WebviewScaffold extends StatefulWidget {
     this.displayZoomControls,
     this.displayZoomControls,
     this.withLocalStorage,
     this.withLocalStorage,
     this.withLocalUrl,
     this.withLocalUrl,
+    this.localUrlScope,
     this.withOverviewMode,
     this.withOverviewMode,
     this.useWideViewPort,
     this.useWideViewPort,
     this.scrollBar,
     this.scrollBar,
@@ -54,6 +55,7 @@ class WebviewScaffold extends StatefulWidget {
   final bool displayZoomControls;
   final bool displayZoomControls;
   final bool withLocalStorage;
   final bool withLocalStorage;
   final bool withLocalUrl;
   final bool withLocalUrl;
+  final String localUrlScope;
   final bool scrollBar;
   final bool scrollBar;
   final bool supportMultipleWindows;
   final bool supportMultipleWindows;
   final bool appCacheEnabled;
   final bool appCacheEnabled;
@@ -157,6 +159,7 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
               displayZoomControls: widget.displayZoomControls,
               displayZoomControls: widget.displayZoomControls,
               withLocalStorage: widget.withLocalStorage,
               withLocalStorage: widget.withLocalStorage,
               withLocalUrl: widget.withLocalUrl,
               withLocalUrl: widget.withLocalUrl,
+              localUrlScope: widget.localUrlScope,
               withOverviewMode: widget.withOverviewMode,
               withOverviewMode: widget.withOverviewMode,
               useWideViewPort: widget.useWideViewPort,
               useWideViewPort: widget.useWideViewPort,
               scrollBar: widget.scrollBar,
               scrollBar: widget.scrollBar,