Skip to content

Commit bf76bac

Browse files
author
rfthejakohead
committed
Fix iOS local URL support (fixes #114)
1 parent 760d6bd commit bf76bac

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

ios/Classes/FlutterWebviewPlugin.m

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,15 @@ - (void)navigate:(FlutterMethodCall*)call {
154154
NSNumber *withLocalUrl = call.arguments[@"withLocalUrl"];
155155
if ( [withLocalUrl boolValue]) {
156156
NSURL *htmlUrl = [NSURL fileURLWithPath:url isDirectory:false];
157+
NSString *localUrlScope = call.arguments[@"localUrlScope"];
157158
if (@available(iOS 9.0, *)) {
158-
[self.webview loadFileURL:htmlUrl allowingReadAccessToURL:htmlUrl];
159+
if(localUrlScope == nil) {
160+
[self.webview loadFileURL:htmlUrl allowingReadAccessToURL:htmlUrl];
161+
}
162+
else {
163+
NSURL *scopeUrl = [NSURL fileURLWithPath:localUrlScope];
164+
[self.webview loadFileURL:htmlUrl allowingReadAccessToURL:scopeUrl];
165+
}
159166
} else {
160167
@throw @"not available on version earlier than ios 9.0";
161168
}
@@ -296,7 +303,8 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigati
296303
if (_enableAppScheme ||
297304
([webView.URL.scheme isEqualToString:@"http"] ||
298305
[webView.URL.scheme isEqualToString:@"https"] ||
299-
[webView.URL.scheme isEqualToString:@"about"])) {
306+
[webView.URL.scheme isEqualToString:@"about"] ||
307+
[webView.URL.scheme isEqualToString:@"file"])) {
300308
if (isInvalid) {
301309
decisionHandler(WKNavigationActionPolicyCancel);
302310
} else {

lib/src/base.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ class FlutterWebviewPlugin {
106106
/// It is always enabled in UIWebView of iOS and can not be disabled.
107107
/// - [withLocalUrl]: allow url as a local path
108108
/// Allow local files on iOs > 9.0
109+
/// - [localUrlScope]: allowed folder for local paths
110+
/// iOS only.
111+
/// If null and withLocalUrl is true, then it will use the url as the scope,
112+
/// allowing only itself to be read.
109113
/// - [scrollBar]: enable or disable scrollbar
110114
/// - [supportMultipleWindows] enable multiple windows support in Android
111115
/// - [invalidUrlRegex] is the regular expression of URLs that web view shouldn't load.
@@ -123,6 +127,7 @@ class FlutterWebviewPlugin {
123127
bool withZoom,
124128
bool withLocalStorage,
125129
bool withLocalUrl,
130+
String localUrlScope,
126131
bool scrollBar,
127132
bool supportMultipleWindows,
128133
bool appCacheEnabled,
@@ -143,6 +148,7 @@ class FlutterWebviewPlugin {
143148
'withZoom': withZoom ?? false,
144149
'withLocalStorage': withLocalStorage ?? true,
145150
'withLocalUrl': withLocalUrl ?? false,
151+
'localUrlScope': localUrlScope,
146152
'scrollBar': scrollBar ?? true,
147153
'supportMultipleWindows': supportMultipleWindows ?? false,
148154
'appCacheEnabled': appCacheEnabled ?? false,

lib/src/webview_scaffold.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class WebviewScaffold extends StatefulWidget {
2424
this.withZoom,
2525
this.withLocalStorage,
2626
this.withLocalUrl,
27+
this.localUrlScope,
2728
this.scrollBar,
2829
this.supportMultipleWindows,
2930
this.appCacheEnabled,
@@ -50,6 +51,7 @@ class WebviewScaffold extends StatefulWidget {
5051
final bool withZoom;
5152
final bool withLocalStorage;
5253
final bool withLocalUrl;
54+
final String localUrlScope;
5355
final bool scrollBar;
5456
final bool supportMultipleWindows;
5557
final bool appCacheEnabled;
@@ -150,6 +152,7 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
150152
withZoom: widget.withZoom,
151153
withLocalStorage: widget.withLocalStorage,
152154
withLocalUrl: widget.withLocalUrl,
155+
localUrlScope: widget.localUrlScope,
153156
scrollBar: widget.scrollBar,
154157
supportMultipleWindows: widget.supportMultipleWindows,
155158
appCacheEnabled: widget.appCacheEnabled,

0 commit comments

Comments
 (0)