Skip to content

Commit c3b3fcf

Browse files
authored
Merge pull request #493 from rafern/master
Fix iOS local URL support (fixes #114)
2 parents ad0bd76 + d351c79 commit c3b3fcf

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ Future<String> loadJS(String name) async {
161161
}
162162
```
163163

164+
### Accessing local files in the file system
165+
Set the `withLocalUrl` option to true in the launch function or in the Webview scaffold to enable support for local URLs.
166+
167+
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.
164168

165169
### Webview Events
166170

@@ -189,6 +193,7 @@ Future<Null> launch(String url, {
189193
bool withZoom: false,
190194
bool withLocalStorage: true,
191195
bool withLocalUrl: true,
196+
String localUrlScope: null,
192197
bool scrollBar: true,
193198
bool supportMultipleWindows: false,
194199
bool appCacheEnabled: false,

ios/Classes/FlutterWebviewPlugin.m

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,15 @@ - (void)navigate:(FlutterMethodCall*)call {
164164
NSNumber *withLocalUrl = call.arguments[@"withLocalUrl"];
165165
if ( [withLocalUrl boolValue]) {
166166
NSURL *htmlUrl = [NSURL fileURLWithPath:url isDirectory:false];
167+
NSString *localUrlScope = call.arguments[@"localUrlScope"];
167168
if (@available(iOS 9.0, *)) {
168-
[self.webview loadFileURL:htmlUrl allowingReadAccessToURL:htmlUrl];
169+
if(localUrlScope == nil) {
170+
[self.webview loadFileURL:htmlUrl allowingReadAccessToURL:htmlUrl];
171+
}
172+
else {
173+
NSURL *scopeUrl = [NSURL fileURLWithPath:localUrlScope];
174+
[self.webview loadFileURL:htmlUrl allowingReadAccessToURL:scopeUrl];
175+
}
169176
} else {
170177
@throw @"not available on version earlier than ios 9.0";
171178
}
@@ -312,7 +319,8 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigati
312319
if (_enableAppScheme ||
313320
([webView.URL.scheme isEqualToString:@"http"] ||
314321
[webView.URL.scheme isEqualToString:@"https"] ||
315-
[webView.URL.scheme isEqualToString:@"about"])) {
322+
[webView.URL.scheme isEqualToString:@"about"] ||
323+
[webView.URL.scheme isEqualToString:@"file"])) {
316324
if (isInvalid) {
317325
decisionHandler(WKNavigationActionPolicyCancel);
318326
} 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.
@@ -128,6 +132,7 @@ class FlutterWebviewPlugin {
128132
bool displayZoomControls,
129133
bool withLocalStorage,
130134
bool withLocalUrl,
135+
String localUrlScope,
131136
bool withOverviewMode,
132137
bool scrollBar,
133138
bool supportMultipleWindows,
@@ -150,6 +155,7 @@ class FlutterWebviewPlugin {
150155
'displayZoomControls': displayZoomControls ?? false,
151156
'withLocalStorage': withLocalStorage ?? true,
152157
'withLocalUrl': withLocalUrl ?? false,
158+
'localUrlScope': localUrlScope,
153159
'scrollBar': scrollBar ?? true,
154160
'supportMultipleWindows': supportMultipleWindows ?? false,
155161
'appCacheEnabled': appCacheEnabled ?? false,

lib/src/webview_scaffold.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class WebviewScaffold extends StatefulWidget {
2525
this.displayZoomControls,
2626
this.withLocalStorage,
2727
this.withLocalUrl,
28+
this.localUrlScope,
2829
this.withOverviewMode,
2930
this.useWideViewPort,
3031
this.scrollBar,
@@ -54,6 +55,7 @@ class WebviewScaffold extends StatefulWidget {
5455
final bool displayZoomControls;
5556
final bool withLocalStorage;
5657
final bool withLocalUrl;
58+
final String localUrlScope;
5759
final bool scrollBar;
5860
final bool supportMultipleWindows;
5961
final bool appCacheEnabled;
@@ -157,6 +159,7 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
157159
displayZoomControls: widget.displayZoomControls,
158160
withLocalStorage: widget.withLocalStorage,
159161
withLocalUrl: widget.withLocalUrl,
162+
localUrlScope: widget.localUrlScope,
160163
withOverviewMode: widget.withOverviewMode,
161164
useWideViewPort: widget.useWideViewPort,
162165
scrollBar: widget.scrollBar,

0 commit comments

Comments
 (0)