Skip to content

Fix iOS local URL support (fixes #114) #493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -189,6 +193,7 @@ Future<Null> launch(String url, {
bool withZoom: false,
bool withLocalStorage: true,
bool withLocalUrl: true,
String localUrlScope: null,
bool scrollBar: true,
bool supportMultipleWindows: false,
bool appCacheEnabled: false,
Expand Down
12 changes: 10 additions & 2 deletions ios/Classes/FlutterWebviewPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,15 @@ - (void)navigate:(FlutterMethodCall*)call {
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";
}
Expand Down Expand Up @@ -296,7 +303,8 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigati
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 {
Expand Down
6 changes: 6 additions & 0 deletions lib/src/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -128,6 +132,7 @@ class FlutterWebviewPlugin {
bool displayZoomControls,
bool withLocalStorage,
bool withLocalUrl,
String localUrlScope,
bool withOverviewMode,
bool scrollBar,
bool supportMultipleWindows,
Expand All @@ -150,6 +155,7 @@ class FlutterWebviewPlugin {
'displayZoomControls': displayZoomControls ?? false,
'withLocalStorage': withLocalStorage ?? true,
'withLocalUrl': withLocalUrl ?? false,
'localUrlScope': localUrlScope,
'scrollBar': scrollBar ?? true,
'supportMultipleWindows': supportMultipleWindows ?? false,
'appCacheEnabled': appCacheEnabled ?? false,
Expand Down
3 changes: 3 additions & 0 deletions lib/src/webview_scaffold.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class WebviewScaffold extends StatefulWidget {
this.displayZoomControls,
this.withLocalStorage,
this.withLocalUrl,
this.localUrlScope,
this.withOverviewMode,
this.useWideViewPort,
this.scrollBar,
Expand Down Expand Up @@ -54,6 +55,7 @@ class WebviewScaffold extends StatefulWidget {
final bool displayZoomControls;
final bool withLocalStorage;
final bool withLocalUrl;
final String localUrlScope;
final bool scrollBar;
final bool supportMultipleWindows;
final bool appCacheEnabled;
Expand Down Expand Up @@ -157,6 +159,7 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
displayZoomControls: widget.displayZoomControls,
withLocalStorage: widget.withLocalStorage,
withLocalUrl: widget.withLocalUrl,
localUrlScope: widget.localUrlScope,
withOverviewMode: widget.withOverviewMode,
useWideViewPort: widget.useWideViewPort,
scrollBar: widget.scrollBar,
Expand Down