Skip to content

Commit ffc7ad8

Browse files
authored
Merge pull request #84 from ypelud/master
Allow local files on iOs > 9.0
2 parents bb198c9 + f49365b commit ffc7ad8

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

ios/Classes/FlutterWebviewPlugin.m

+14-4
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,20 @@ - (CGRect)parseRect:(NSDictionary *)rect {
101101

102102
- (void)navigate:(FlutterMethodCall*)call {
103103
if (self.webview != nil) {
104-
NSString *url = call.arguments[@"url"];
105-
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
106-
[self.webview loadRequest:request];
107-
}
104+
NSString *url = call.arguments[@"url"];
105+
NSNumber *withLocalUrl = call.arguments[@"withLocalUrl"];
106+
if ( [withLocalUrl boolValue]) {
107+
NSURL *htmlUrl = [NSURL fileURLWithPath:url isDirectory:false];
108+
if (@available(iOS 9.0, *)) {
109+
[self.webview loadFileURL:htmlUrl allowingReadAccessToURL:htmlUrl];
110+
} else {
111+
@throw @"not available on version earlier than ios 9.0";
112+
}
113+
} else {
114+
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
115+
[self.webview loadRequest:request];
116+
}
117+
}
108118
}
109119

110120
- (void)evalJavascript:(FlutterMethodCall*)call

lib/src/base.dart

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import 'dart:async';
22
import 'dart:ui';
33

4-
import 'package:flutter/services.dart';
54
import 'package:flutter/material.dart';
5+
import 'package:flutter/services.dart';
66

77
const _kChannel = 'flutter_webview_plugin';
88

@@ -71,6 +71,8 @@ class FlutterWebviewPlugin {
7171
/// - [withLocalStorage] enable localStorage API on Webview
7272
/// Currently Android only.
7373
/// It is always enabled in UIWebView of iOS and can not be disabled.
74+
/// - [withLocalUrl]: allow url as a local path
75+
/// Allow local files on iOs > 9.0
7476
Future<Null> launch(String url,
7577
{bool withJavascript,
7678
bool clearCache,
@@ -80,7 +82,8 @@ class FlutterWebviewPlugin {
8082
Rect rect,
8183
String userAgent,
8284
bool withZoom,
83-
bool withLocalStorage}) async {
85+
bool withLocalStorage,
86+
bool withLocalUrl}) async {
8487
Map<String, dynamic> args = {
8588
"url": url,
8689
"withJavascript": withJavascript ?? true,
@@ -90,7 +93,8 @@ class FlutterWebviewPlugin {
9093
"enableAppScheme": enableAppScheme ?? true,
9194
"userAgent": userAgent,
9295
"withZoom": withZoom ?? false,
93-
"withLocalStorage": withLocalStorage ?? true
96+
"withLocalStorage": withLocalStorage ?? true,
97+
"withLocalUrl": withLocalUrl ?? false
9498
};
9599
if (rect != null) {
96100
args["rect"] = {

lib/src/webview_scaffold.dart

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class WebviewScaffold extends StatefulWidget {
1818
final Widget bottomNavigationBar;
1919
final bool withZoom;
2020
final bool withLocalStorage;
21+
final bool withLocalUrl;
2122

2223
WebviewScaffold(
2324
{Key key,
@@ -32,7 +33,8 @@ class WebviewScaffold extends StatefulWidget {
3233
this.persistentFooterButtons,
3334
this.bottomNavigationBar,
3435
this.withZoom,
35-
this.withLocalStorage})
36+
this.withLocalStorage,
37+
this.withLocalUrl})
3638
: super(key: key);
3739

3840
@override
@@ -67,7 +69,8 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
6769
userAgent: widget.userAgent,
6870
rect: _rect,
6971
withZoom: widget.withZoom,
70-
withLocalStorage: widget.withLocalStorage);
72+
withLocalStorage: widget.withLocalStorage,
73+
withLocalUrl: widget.withLocalUrl);
7174
} else {
7275
Rect rect = _buildRect(context);
7376
if (_rect != rect) {

0 commit comments

Comments
 (0)