Skip to content

Commit 27ea093

Browse files
authored
Merge pull request #1 from dart-flitter/master
Update
2 parents ef00a5d + 172c27c commit 27ea093

File tree

7 files changed

+1442
-1482
lines changed

7 files changed

+1442
-1482
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.1.5
2+
3+
- iOS use WKWebView instead of UIWebView
4+
15
# 0.1.4
26

37
- support localstorage for ANDROID

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
Plugin that allow Flutter to communicate with a native WebView.
77

8+
***Warning:***
9+
The webview is not integrated in the widget tree, it is a native view on top of the flutter view.
10+
you won't be able to use snackbars, dialogs ...
11+
812
## Getting Started
913

1014
For help getting started with Flutter, view our online [documentation](http://flutter.io/).

example/ios/Flutter/flutter_assets/LICENSE

Lines changed: 1359 additions & 1425 deletions
Large diffs are not rendered by default.
Binary file not shown.

ios/Classes/FlutterWebviewPlugin.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#import <Flutter/Flutter.h>
2+
#import <WebKit/WebKit.h>
23

34
static FlutterMethodChannel *channel;
45

56
@interface FlutterWebviewPlugin : NSObject<FlutterPlugin>
67
@property (nonatomic, retain) UIViewController *viewController;
7-
@property (nonatomic, retain) UIWebView *webview;
8+
@property (nonatomic, retain) WKWebView *webview;
89
@end

ios/Classes/FlutterWebviewPlugin.m

Lines changed: 72 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
44

55
// UIWebViewDelegate
6-
@interface FlutterWebviewPlugin() <UIWebViewDelegate> {
6+
@interface FlutterWebviewPlugin() <WKNavigationDelegate, UIScrollViewDelegate> {
77
BOOL _enableAppScheme;
8+
BOOL _enableZoom;
89
}
910
@end
1011

@@ -39,7 +40,9 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
3940
[self closeWebView];
4041
result(nil);
4142
} else if ([@"eval" isEqualToString:call.method]) {
42-
result([self evalJavascript:call]);
43+
[self evalJavascript:call completionHandler:^(NSString * response) {
44+
result(response);
45+
}];
4346
} else if ([@"resize" isEqualToString:call.method]) {
4447
[self resize:call];
4548
result(nil);
@@ -49,7 +52,6 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
4952
}
5053

5154
- (void)initWebview:(FlutterMethodCall*)call {
52-
// NSNumber *withJavascript = call.arguments[@"withJavascript"];
5355
NSNumber *clearCache = call.arguments[@"clearCache"];
5456
NSNumber *clearCookies = call.arguments[@"clearCookies"];
5557
NSNumber *hidden = call.arguments[@"hidden"];
@@ -58,7 +60,6 @@ - (void)initWebview:(FlutterMethodCall*)call {
5860
NSString *userAgent = call.arguments[@"userAgent"];
5961
NSNumber *withZoom = call.arguments[@"withZoom"];
6062

61-
//
6263
if (clearCache != (id)[NSNull null] && [clearCache boolValue]) {
6364
[[NSURLCache sharedURLCache] removeAllCachedResponses];
6465
}
@@ -79,18 +80,15 @@ - (void)initWebview:(FlutterMethodCall*)call {
7980
rc = self.viewController.view.bounds;
8081
}
8182

82-
self.webview = [[UIWebView alloc] initWithFrame:rc];
83-
self.webview.delegate = self;
84-
85-
if (withZoom != (id)[NSNull null] && [withZoom boolValue]) {
86-
self.webview.scalesPageToFit = YES;
87-
}
88-
89-
if (hidden != (id)[NSNull null] && [hidden boolValue]) {
90-
self.webview.hidden = YES;
91-
}
83+
self.webview = [[WKWebView alloc] initWithFrame:rc];
84+
self.webview.navigationDelegate = self;
85+
self.webview.scrollView.delegate = self;
86+
self.webview.hidden = [hidden boolValue];
87+
88+
_enableZoom = [withZoom boolValue];
89+
9290
[self.viewController.view addSubview:self.webview];
93-
91+
9492
[self navigate:call];
9593
}
9694

@@ -102,73 +100,92 @@ - (CGRect)parseRect:(NSDictionary *)rect {
102100
}
103101

104102
- (void)navigate:(FlutterMethodCall*)call {
105-
NSString *url = call.arguments[@"url"];
106-
107-
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
108-
[self.webview loadRequest:request];
103+
if (self.webview != nil) {
104+
NSString *url = call.arguments[@"url"];
105+
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
106+
[self.webview loadRequest:request];
107+
}
109108
}
110109

111-
- (NSString *)evalJavascript:(FlutterMethodCall*)call {
112-
NSString *code = call.arguments[@"code"];
113-
114-
NSString *result = [self.webview stringByEvaluatingJavaScriptFromString:code];
115-
return result;
110+
- (void)evalJavascript:(FlutterMethodCall*)call
111+
completionHandler:(void (^_Nullable)(NSString * response))completionHandler {
112+
if (self.webview != nil) {
113+
NSString *code = call.arguments[@"code"];
114+
[self.webview evaluateJavaScript:code
115+
completionHandler:^(id _Nullable response, NSError * _Nullable error) {
116+
completionHandler([NSString stringWithFormat:@"%@", response]);
117+
}];
118+
} else {
119+
completionHandler(nil);
120+
}
116121
}
117122

118123
- (void)resize:(FlutterMethodCall*)call {
119-
NSDictionary *rect = call.arguments[@"rect"];
120-
CGRect rc = [self parseRect:rect];
121-
self.webview.frame = rc;
124+
if (self.webview != nil) {
125+
NSDictionary *rect = call.arguments[@"rect"];
126+
CGRect rc = [self parseRect:rect];
127+
self.webview.frame = rc;
128+
}
122129
}
123130

124131
- (void)closeWebView {
125-
[self.webview stopLoading];
126-
[self.webview removeFromSuperview];
127-
self.webview.delegate = nil;
128-
self.webview = nil;
129-
130-
// manually trigger onDestroy
131-
[channel invokeMethod:@"onDestroy" arguments:nil];
132+
if (self.webview != nil) {
133+
[self.webview stopLoading];
134+
[self.webview removeFromSuperview];
135+
self.webview.navigationDelegate = nil;
136+
self.webview = nil;
137+
138+
// manually trigger onDestroy
139+
[channel invokeMethod:@"onDestroy" arguments:nil];
140+
}
132141
}
133142

143+
#pragma mark -- WkWebView Delegate
144+
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
145+
decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
134146

135-
#pragma mark -- WebView Delegate
136-
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
137-
id data = @{@"url": request.URL.absoluteString,
147+
id data = @{@"url": navigationAction.request.URL.absoluteString,
138148
@"type": @"shouldStart",
139-
@"navigationType": [NSNumber numberWithInt:navigationType]};
149+
@"navigationType": [NSNumber numberWithInt:navigationAction.navigationType]};
140150
[channel invokeMethod:@"onState" arguments:data];
141-
142-
if (navigationType == UIWebViewNavigationTypeBackForward)
151+
152+
if (navigationAction.navigationType == WKNavigationTypeBackForward) {
143153
[channel invokeMethod:@"onBackPressed" arguments:nil];
144-
else {
145-
id data = @{@"url": request.URL.absoluteString};
154+
} else {
155+
id data = @{@"url": navigationAction.request.URL.absoluteString};
146156
[channel invokeMethod:@"onUrlChanged" arguments:data];
147157
}
148-
149-
if (_enableAppScheme)
150-
return YES;
151158

152-
// disable some scheme
153-
return [request.URL.scheme isEqualToString:@"http"] ||
154-
[request.URL.scheme isEqualToString:@"https"] ||
155-
[request.URL.scheme isEqualToString:@"about"];
159+
if (_enableAppScheme ||
160+
([webView.URL.scheme isEqualToString:@"http"] ||
161+
[webView.URL.scheme isEqualToString:@"https"] ||
162+
[webView.URL.scheme isEqualToString:@"about"])) {
163+
decisionHandler(WKNavigationActionPolicyAllow);
164+
} else {
165+
decisionHandler(WKNavigationActionPolicyCancel);
166+
}
156167
}
157168

158-
-(void)webViewDidStartLoad:(UIWebView *)webView {
159-
[channel invokeMethod:@"onState" arguments:@{@"type": @"startLoad", @"url": webView.request.URL.absoluteString}];
169+
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
170+
[channel invokeMethod:@"onState" arguments:@{@"type": @"startLoad", @"url": webView.URL.absoluteString}];
160171
}
161172

162-
- (void)webViewDidFinishLoad:(UIWebView *)webView {
163-
[channel invokeMethod:@"onState" arguments:@{@"type": @"finishLoad", @"url": webView.request.URL.absoluteString}];
173+
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
174+
[channel invokeMethod:@"onState" arguments:@{@"type": @"finishLoad", @"url": webView.URL.absoluteString}];
164175
}
165176

166-
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
177+
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
167178
id data = [FlutterError errorWithCode:[NSString stringWithFormat:@"%ld", error.code]
168179
message:error.localizedDescription
169180
details:error.localizedFailureReason];
170181
[channel invokeMethod:@"onError" arguments:data];
171182
}
172183

173-
#pragma mark -- WkWebView Delegate
184+
#pragma mark -- UIScrollViewDelegate
185+
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
186+
if (scrollView.pinchGestureRecognizer.isEnabled != _enableZoom) {
187+
scrollView.pinchGestureRecognizer.enabled = _enableZoom;
188+
}
189+
}
190+
174191
@end

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ authors:
55
- Toufik Zitouni <[email protected]>
66
77
homepage: https://github.com/dart-flitter/flutter_webview_plugin
8-
version: 0.1.4
8+
version: 0.1.5
99

1010
environment:
1111
sdk: ">=1.8.0 <2.0.0"

0 commit comments

Comments
 (0)