-
Notifications
You must be signed in to change notification settings - Fork 937
Expand file tree
/
Copy pathFlutterWebviewPlugin.m
More file actions
540 lines (461 loc) · 21.6 KB
/
FlutterWebviewPlugin.m
File metadata and controls
540 lines (461 loc) · 21.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#import "FlutterWebviewPlugin.h"
#import "WebviewJavaScriptChannelHandler.h"
static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
// UIWebViewDelegate
@interface FlutterWebviewPlugin() <WKNavigationDelegate, UIScrollViewDelegate, WKUIDelegate> {
BOOL _enableAppScheme;
BOOL _enableZoom;
NSString* _invalidUrlRegex;
NSMutableSet* _javaScriptChannelNames;
NSNumber* _ignoreSSLErrors;
}
@end
@implementation FlutterWebviewPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
channel = [FlutterMethodChannel
methodChannelWithName:CHANNEL_NAME
binaryMessenger:[registrar messenger]];
UIViewController *viewController = [UIApplication sharedApplication].delegate.window.rootViewController;
FlutterWebviewPlugin* instance = [[FlutterWebviewPlugin alloc] initWithViewController:viewController];
[registrar addMethodCallDelegate:instance channel:channel];
}
- (instancetype)initWithViewController:(UIViewController *)viewController {
self = [super init];
if (self) {
self.viewController = viewController;
}
return self;
}
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([@"launch" isEqualToString:call.method]) {
if (!self.webview)
[self initWebview:call withResult:result];
else
[self navigate:call];
result(nil);
} else if ([@"close" isEqualToString:call.method]) {
[self closeWebView];
result(nil);
} else if ([@"eval" isEqualToString:call.method]) {
[self evalJavascript:call completionHandler:^(NSString * response) {
result(response);
}];
} else if ([@"resize" isEqualToString:call.method]) {
[self resize:call];
result(nil);
} else if ([@"reloadUrl" isEqualToString:call.method]) {
[self reloadUrl:call];
result(nil);
} else if ([@"show" isEqualToString:call.method]) {
[self show];
result(nil);
} else if ([@"hide" isEqualToString:call.method]) {
[self hide];
result(nil);
} else if ([@"stopLoading" isEqualToString:call.method]) {
[self stopLoading];
result(nil);
} else if ([@"cleanCookies" isEqualToString:call.method]) {
[self cleanCookies:result];
} else if ([@"back" isEqualToString:call.method]) {
[self back];
result(nil);
} else if ([@"forward" isEqualToString:call.method]) {
[self forward];
result(nil);
} else if ([@"reload" isEqualToString:call.method]) {
[self reload];
result(nil);
} else if ([@"canGoBack" isEqualToString:call.method]) {
[self onCanGoBack:call result:result];
} else if ([@"canGoForward" isEqualToString:call.method]) {
[self onCanGoForward:call result:result];
} else if ([@"cleanCache" isEqualToString:call.method]) {
[self cleanCache:result];
} else {
result(FlutterMethodNotImplemented);
}
}
- (void)initWebview:(FlutterMethodCall*)call withResult:(FlutterResult)result {
NSNumber *clearCache = call.arguments[@"clearCache"];
NSNumber *clearCookies = call.arguments[@"clearCookies"];
NSNumber *hidden = call.arguments[@"hidden"];
NSDictionary *rect = call.arguments[@"rect"];
_enableAppScheme = call.arguments[@"enableAppScheme"];
NSString *userAgent = call.arguments[@"userAgent"];
NSNumber *withZoom = call.arguments[@"withZoom"];
NSNumber *scrollBar = call.arguments[@"scrollBar"];
NSNumber *withJavascript = call.arguments[@"withJavascript"];
_invalidUrlRegex = call.arguments[@"invalidUrlRegex"];
_ignoreSSLErrors = call.arguments[@"ignoreSSLErrors"];
_javaScriptChannelNames = [[NSMutableSet alloc] init];
WKUserContentController* userContentController = [[WKUserContentController alloc] init];
if ([call.arguments[@"javascriptChannelNames"] isKindOfClass:[NSArray class]]) {
NSArray* javaScriptChannelNames = call.arguments[@"javascriptChannelNames"];
[_javaScriptChannelNames addObjectsFromArray:javaScriptChannelNames];
[self registerJavaScriptChannels:_javaScriptChannelNames controller:userContentController];
}
if (clearCache != (id)[NSNull null] && [clearCache boolValue]) {
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[self cleanCache:result];
}
if (clearCookies != (id)[NSNull null] && [clearCookies boolValue]) {
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [storage cookies])
{
[storage deleteCookie:cookie];
}
[self cleanCookies:result];
}
if (userAgent != (id)[NSNull null]) {
[[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent": userAgent}];
}
CGRect rc;
if (rect != nil) {
rc = [self parseRect:rect];
} else {
rc = self.viewController.view.bounds;
}
WKWebViewConfiguration* configuration = [[WKWebViewConfiguration alloc] init];
configuration.userContentController = userContentController;
self.webview = [[WKWebView alloc] initWithFrame:rc configuration:configuration];
self.webview.UIDelegate = self;
self.webview.navigationDelegate = self;
self.webview.scrollView.delegate = self;
self.webview.hidden = [hidden boolValue];
self.webview.scrollView.showsHorizontalScrollIndicator = [scrollBar boolValue];
self.webview.scrollView.showsVerticalScrollIndicator = [scrollBar boolValue];
if (@available(iOS 9.0, *)) {
if (userAgent != (id)[NSNull null]) {
self.webview.customUserAgent = userAgent;
}
}
[self.webview addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];
WKPreferences* preferences = [[self.webview configuration] preferences];
if ([withJavascript boolValue]) {
[preferences setJavaScriptEnabled:YES];
} else {
[preferences setJavaScriptEnabled:NO];
}
_enableZoom = [withZoom boolValue];
UIViewController* presentedViewController = self.viewController.presentedViewController;
UIViewController* currentViewController = presentedViewController != nil ? presentedViewController : self.viewController;
[currentViewController.view addSubview:self.webview];
[self navigate:call];
}
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {
if ([_ignoreSSLErrors boolValue]){
SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;
CFDataRef exceptions = SecTrustCopyExceptions(serverTrust);
SecTrustSetExceptions(serverTrust, exceptions);
CFRelease(exceptions);
completionHandler(NSURLSessionAuthChallengeUseCredential,
[NSURLCredential credentialForTrust:serverTrust]);
}
else {
completionHandler(NSURLSessionAuthChallengePerformDefaultHandling,nil);
}
}
- (CGRect)parseRect:(NSDictionary *)rect {
return CGRectMake([[rect valueForKey:@"left"] doubleValue],
[[rect valueForKey:@"top"] doubleValue],
[[rect valueForKey:@"width"] doubleValue],
[[rect valueForKey:@"height"] doubleValue]);
}
- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
id xDirection = @{@"xDirection": @(scrollView.contentOffset.x) };
[channel invokeMethod:@"onScrollXChanged" arguments:xDirection];
id yDirection = @{@"yDirection": @(scrollView.contentOffset.y) };
[channel invokeMethod:@"onScrollYChanged" arguments:yDirection];
}
- (void)navigate:(FlutterMethodCall*)call {
if (self.webview != nil) {
NSString *url = call.arguments[@"url"];
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, *)) {
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";
}
} else {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
NSDictionary *headers = call.arguments[@"headers"];
if (headers != nil) {
[request setAllHTTPHeaderFields:headers];
}
[self.webview loadRequest:request];
}
}
}
- (void)evalJavascript:(FlutterMethodCall*)call
completionHandler:(void (^_Nullable)(NSString * response))completionHandler {
if (self.webview != nil) {
NSString *code = call.arguments[@"code"];
[self.webview evaluateJavaScript:code
completionHandler:^(id _Nullable response, NSError * _Nullable error) {
completionHandler([NSString stringWithFormat:@"%@", response]);
}];
} else {
completionHandler(nil);
}
}
- (void)resize:(FlutterMethodCall*)call {
if (self.webview != nil) {
NSDictionary *rect = call.arguments[@"rect"];
CGRect rc = [self parseRect:rect];
self.webview.frame = rc;
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"estimatedProgress"] && object == self.webview) {
[channel invokeMethod:@"onProgressChanged" arguments:@{@"progress": @(self.webview.estimatedProgress)}];
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
- (void)closeWebView {
if (self.webview != nil) {
[self.webview stopLoading];
[self.webview removeFromSuperview];
self.webview.navigationDelegate = nil;
[self.webview removeObserver:self forKeyPath:@"estimatedProgress"];
self.webview = nil;
// manually trigger onDestroy
[channel invokeMethod:@"onDestroy" arguments:nil];
}
}
- (void)reloadUrl:(FlutterMethodCall*)call {
if (self.webview != nil) {
NSString *url = call.arguments[@"url"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
NSDictionary *headers = call.arguments[@"headers"];
if (headers != nil) {
[request setAllHTTPHeaderFields:headers];
}
[self.webview loadRequest:request];
}
}
- (void)cleanCookies:(FlutterResult)result {
if(self.webview != nil) {
[[NSURLSession sharedSession] resetWithCompletionHandler:^{
}];
if (@available(iOS 9.0, *)) {
NSSet<NSString *> *websiteDataTypes = [NSSet setWithObject:WKWebsiteDataTypeCookies];
WKWebsiteDataStore *dataStore = [WKWebsiteDataStore defaultDataStore];
void (^deleteAndNotify)(NSArray<WKWebsiteDataRecord *> *) =
^(NSArray<WKWebsiteDataRecord *> *cookies) {
[dataStore removeDataOfTypes:websiteDataTypes
forDataRecords:cookies
completionHandler:^{
result(nil);
}];
};
[dataStore fetchDataRecordsOfTypes:websiteDataTypes completionHandler:deleteAndNotify];
} else {
// support for iOS8 tracked in https://github.com/flutter/flutter/issues/27624.
NSLog(@"Clearing cookies is not supported for Flutter WebViews prior to iOS 9.");
}
}
}
- (void)cleanCache:(FlutterResult)result {
if (self.webview != nil) {
if (@available(iOS 9.0, *)) {
NSSet* cacheDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
WKWebsiteDataStore* dataStore = [WKWebsiteDataStore defaultDataStore];
NSDate* dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
[dataStore removeDataOfTypes:cacheDataTypes
modifiedSince:dateFrom
completionHandler:^{
result(nil);
}];
} else {
// support for iOS8 tracked in https://github.com/flutter/flutter/issues/27624.
NSLog(@"Clearing cache is not supported for Flutter WebViews prior to iOS 9.");
}
}
}
- (void)show {
if (self.webview != nil) {
self.webview.hidden = false;
}
}
- (void)hide {
if (self.webview != nil) {
self.webview.hidden = true;
}
}
- (void)stopLoading {
if (self.webview != nil) {
[self.webview stopLoading];
}
}
- (void)back {
if (self.webview != nil) {
[self.webview goBack];
}
}
- (void)onCanGoBack:(FlutterMethodCall*)call result:(FlutterResult)result {
BOOL canGoBack = [self.webview canGoBack];
result([NSNumber numberWithBool:canGoBack]);
}
- (void)onCanGoForward:(FlutterMethodCall*)call result:(FlutterResult)result {
BOOL canGoForward = [self.webview canGoForward];
result([NSNumber numberWithBool:canGoForward]);
}
- (void)forward {
if (self.webview != nil) {
[self.webview goForward];
}
}
- (void)reload {
if (self.webview != nil) {
[self.webview reload];
}
}
- (bool)checkInvalidUrl:(NSURL*)url {
NSString* urlString = url != nil ? [url absoluteString] : nil;
if (![_invalidUrlRegex isEqual:[NSNull null]] && urlString != nil) {
NSError* error = NULL;
NSRegularExpression* regex =
[NSRegularExpression regularExpressionWithPattern:_invalidUrlRegex
options:NSRegularExpressionCaseInsensitive
error:&error];
NSTextCheckingResult* match = [regex firstMatchInString:urlString
options:0
range:NSMakeRange(0, [urlString length])];
return match != nil;
} else {
return false;
}
}
#pragma mark -- WkWebView Delegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
BOOL isInvalid = [self checkInvalidUrl: navigationAction.request.URL];
id data = @{@"url": navigationAction.request.URL.absoluteString,
@"type": isInvalid ? @"abortLoad" : @"shouldStart",
@"navigationType": [NSNumber numberWithInteger:navigationAction.navigationType]};
[channel invokeMethod:@"onState" arguments:data];
if (navigationAction.navigationType == WKNavigationTypeBackForward) {
[channel invokeMethod:@"onBackPressed" arguments:nil];
} else if (!isInvalid) {
id data = @{@"url": navigationAction.request.URL.absoluteString};
[channel invokeMethod:@"onUrlChanged" arguments:data];
}
if (_enableAppScheme ||
([webView.URL.scheme isEqualToString:@"http"] ||
[webView.URL.scheme isEqualToString:@"https"] ||
[webView.URL.scheme isEqualToString:@"about"] ||
[webView.URL.scheme isEqualToString:@"file"])) {
if (isInvalid) {
decisionHandler(WKNavigationActionPolicyCancel);
} else {
decisionHandler(WKNavigationActionPolicyAllow);
}
} else {
decisionHandler(WKNavigationActionPolicyCancel);
}
}
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration
forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures {
if (!navigationAction.targetFrame.isMainFrame) {
[webView loadRequest:navigationAction.request];
}
return nil;
}
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
[channel invokeMethod:@"onState" arguments:@{@"type": @"startLoad", @"url": webView.URL.absoluteString}];
}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
NSString* url = webView.URL == nil ? @"?" : webView.URL.absoluteString;
[channel invokeMethod:@"onHttpError" arguments:@{@"code": [NSString stringWithFormat:@"%ld", error.code], @"url": url}];
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
[channel invokeMethod:@"onState" arguments:@{@"type": @"finishLoad", @"url": webView.URL.absoluteString}];
}
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
[channel invokeMethod:@"onHttpError" arguments:@{@"code": [NSString stringWithFormat:@"%ld", error.code], @"error": error.localizedDescription}];
}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
if ([navigationResponse.response isKindOfClass:[NSHTTPURLResponse class]]) {
NSHTTPURLResponse * response = (NSHTTPURLResponse *)navigationResponse.response;
if (response.statusCode >= 400) {
[channel invokeMethod:@"onHttpError" arguments:@{@"code": [NSString stringWithFormat:@"%ld", response.statusCode], @"url": webView.URL.absoluteString}];
}
}
decisionHandler(WKNavigationResponsePolicyAllow);
}
- (void)registerJavaScriptChannels:(NSSet*)channelNames
controller:(WKUserContentController*)userContentController {
for (NSString* channelName in channelNames) {
FLTCommunityJavaScriptChannel* _channel =
[[FLTCommunityJavaScriptChannel alloc] initWithMethodChannel: channel
javaScriptChannelName:channelName];
[userContentController addScriptMessageHandler:_channel name:channelName];
NSString* wrapperSource = [NSString
stringWithFormat:@"window.%@ = webkit.messageHandlers.%@;", channelName, channelName];
WKUserScript* wrapperScript =
[[WKUserScript alloc] initWithSource:wrapperSource
injectionTime:WKUserScriptInjectionTimeAtDocumentStart
forMainFrameOnly:NO];
[userContentController addUserScript:wrapperScript];
}
}
#pragma mark -- UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
if (scrollView.pinchGestureRecognizer.isEnabled != _enableZoom) {
scrollView.pinchGestureRecognizer.enabled = _enableZoom;
}
}
#pragma mark -- WKUIDelegate
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
message:message
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
completionHandler();
}]];
[self.viewController presentViewController:alert animated:YES completion:nil];
}
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
message:message
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
completionHandler(NO);
}]];
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
completionHandler(YES);
}]];
[self.viewController presentViewController:alert animated:YES completion:nil];
}
- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString *result))completionHandler
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
message:prompt
preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = prompt;
textField.secureTextEntry = NO;
textField.text = defaultText;
}];
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
completionHandler(nil);
}]];
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
completionHandler([alert.textFields.firstObject text]);
}]];
[self.viewController presentViewController:alert animated:YES completion:nil];
}
@end