Browse Source

Fix iOS local URL support (fixes #114)

rfthejakohead 6 years ago
parent
commit
faf31452e1
3 changed files with 19 additions and 2 deletions
  1. 10 2
      ios/Classes/FlutterWebviewPlugin.m
  2. 6 0
      lib/src/base.dart
  3. 3 0
      lib/src/webview_scaffold.dart

+ 10 - 2
ios/Classes/FlutterWebviewPlugin.m

@@ -154,8 +154,15 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
             NSNumber *withLocalUrl = call.arguments[@"withLocalUrl"];
             if ( [withLocalUrl boolValue]) {
                 NSURL *htmlUrl = [NSURL fileURLWithPath:url isDirectory:false];
+                NSString *localUrlScope = call.arguments[@"localUrlScope"];
                 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 {
                     @throw @"not available on version earlier than ios 9.0";
                 }
@@ -296,7 +303,8 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
     if (_enableAppScheme ||
         ([webView.URL.scheme isEqualToString:@"http"] ||
          [webView.URL.scheme isEqualToString:@"https"] ||
-         [webView.URL.scheme isEqualToString:@"about"])) {
+         [webView.URL.scheme isEqualToString:@"about"] ||
+         [webView.URL.scheme isEqualToString:@"file"])) {
          if (isInvalid) {
             decisionHandler(WKNavigationActionPolicyCancel);
          } 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.
   /// - [withLocalUrl]: allow url as a local path
   ///     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
   /// - [supportMultipleWindows] enable multiple windows support in Android
   /// - [invalidUrlRegex] is the regular expression of URLs that web view shouldn't load.
@@ -123,6 +127,7 @@ class FlutterWebviewPlugin {
     bool withZoom,
     bool withLocalStorage,
     bool withLocalUrl,
+    String localUrlScope,
     bool scrollBar,
     bool supportMultipleWindows,
     bool appCacheEnabled,
@@ -143,6 +148,7 @@ class FlutterWebviewPlugin {
       'withZoom': withZoom ?? false,
       'withLocalStorage': withLocalStorage ?? true,
       'withLocalUrl': withLocalUrl ?? false,
+      'localUrlScope': localUrlScope,
       'scrollBar': scrollBar ?? true,
       'supportMultipleWindows': supportMultipleWindows ?? false,
       'appCacheEnabled': appCacheEnabled ?? false,

+ 3 - 0
lib/src/webview_scaffold.dart

@@ -24,6 +24,7 @@ class WebviewScaffold extends StatefulWidget {
     this.withZoom,
     this.withLocalStorage,
     this.withLocalUrl,
+    this.localUrlScope,
     this.scrollBar,
     this.supportMultipleWindows,
     this.appCacheEnabled,
@@ -50,6 +51,7 @@ class WebviewScaffold extends StatefulWidget {
   final bool withZoom;
   final bool withLocalStorage;
   final bool withLocalUrl;
+  final String localUrlScope;
   final bool scrollBar;
   final bool supportMultipleWindows;
   final bool appCacheEnabled;
@@ -150,6 +152,7 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
               withZoom: widget.withZoom,
               withLocalStorage: widget.withLocalStorage,
               withLocalUrl: widget.withLocalUrl,
+              localUrlScope: widget.localUrlScope,
               scrollBar: widget.scrollBar,
               supportMultipleWindows: widget.supportMultipleWindows,
               appCacheEnabled: widget.appCacheEnabled,