3
3
static NSString *const CHANNEL_NAME = @" flutter_webview_plugin" ;
4
4
5
5
// UIWebViewDelegate
6
- @interface FlutterWebviewPlugin () <UIWebViewDelegate > {
6
+ @interface FlutterWebviewPlugin () <WKNavigationDelegate , UIScrollViewDelegate > {
7
7
BOOL _enableAppScheme;
8
+ BOOL _enableZoom;
8
9
}
9
10
@end
10
11
@@ -39,7 +40,9 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
39
40
[self closeWebView ];
40
41
result (nil );
41
42
} else if ([@" eval" isEqualToString: call.method]) {
42
- result ([self evalJavascript: call]);
43
+ [self evalJavascript: call completionHandler: ^(NSString * response) {
44
+ result (response);
45
+ }];
43
46
} else if ([@" resize" isEqualToString: call.method]) {
44
47
[self resize: call];
45
48
result (nil );
@@ -49,7 +52,6 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
49
52
}
50
53
51
54
- (void )initWebview : (FlutterMethodCall*)call {
52
- // NSNumber *withJavascript = call.arguments[@"withJavascript"];
53
55
NSNumber *clearCache = call.arguments [@" clearCache" ];
54
56
NSNumber *clearCookies = call.arguments [@" clearCookies" ];
55
57
NSNumber *hidden = call.arguments [@" hidden" ];
@@ -58,7 +60,6 @@ - (void)initWebview:(FlutterMethodCall*)call {
58
60
NSString *userAgent = call.arguments [@" userAgent" ];
59
61
NSNumber *withZoom = call.arguments [@" withZoom" ];
60
62
61
- //
62
63
if (clearCache != (id )[NSNull null ] && [clearCache boolValue ]) {
63
64
[[NSURLCache sharedURLCache ] removeAllCachedResponses ];
64
65
}
@@ -79,18 +80,15 @@ - (void)initWebview:(FlutterMethodCall*)call {
79
80
rc = self.viewController .view .bounds ;
80
81
}
81
82
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
+
92
90
[self .viewController.view addSubview: self .webview];
93
-
91
+
94
92
[self navigate: call];
95
93
}
96
94
@@ -102,73 +100,92 @@ - (CGRect)parseRect:(NSDictionary *)rect {
102
100
}
103
101
104
102
- (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
+ }
109
108
}
110
109
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
+ }
116
121
}
117
122
118
123
- (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
+ }
122
129
}
123
130
124
131
- (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
+ }
132
141
}
133
142
143
+ #pragma mark -- WkWebView Delegate
144
+ - (void )webView : (WKWebView *)webView decidePolicyForNavigationAction : (WKNavigationAction *)navigationAction
145
+ decisionHandler : (void (^)(WKNavigationActionPolicy ))decisionHandler {
134
146
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 ,
138
148
@" type" : @" shouldStart" ,
139
- @" navigationType" : [NSNumber numberWithInt: navigationType]};
149
+ @" navigationType" : [NSNumber numberWithInt: navigationAction. navigationType]};
140
150
[channel invokeMethod: @" onState" arguments: data];
141
-
142
- if (navigationType == UIWebViewNavigationTypeBackForward)
151
+
152
+ if (navigationAction. navigationType == WKNavigationTypeBackForward ) {
143
153
[channel invokeMethod: @" onBackPressed" arguments: nil ];
144
- else {
145
- id data = @{@" url" : request.URL .absoluteString };
154
+ } else {
155
+ id data = @{@" url" : navigationAction. request .URL .absoluteString };
146
156
[channel invokeMethod: @" onUrlChanged" arguments: data];
147
157
}
148
-
149
- if (_enableAppScheme)
150
- return YES ;
151
158
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
+ }
156
167
}
157
168
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 }];
160
171
}
161
172
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 }];
164
175
}
165
176
166
- - (void )webView : (UIWebView *)webView didFailLoadWithError : (NSError *)error {
177
+ - (void )webView : (WKWebView *)webView didFailNavigation : ( WKNavigation *) navigation withError : (NSError *)error {
167
178
id data = [FlutterError errorWithCode: [NSString stringWithFormat: @" %ld " , error.code]
168
179
message: error.localizedDescription
169
180
details: error.localizedFailureReason];
170
181
[channel invokeMethod: @" onError" arguments: data];
171
182
}
172
183
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
+
174
191
@end
0 commit comments