@@ -74,26 +74,26 @@ - (instancetype)initWithFrame:(CGRect)frame
74
74
binaryMessenger : (NSObject <FlutterBinaryMessenger>*)messenger {
75
75
if (self = [super init ]) {
76
76
_viewId = viewId;
77
-
77
+
78
78
NSString * channelName = [NSString stringWithFormat: @" plugins.flutter.io/webview_%lld " , viewId];
79
79
_channel = [FlutterMethodChannel methodChannelWithName: channelName binaryMessenger: messenger];
80
80
_javaScriptChannelNames = [[NSMutableSet alloc ] init ];
81
-
81
+
82
82
WKUserContentController * userContentController = [[WKUserContentController alloc ] init ];
83
83
if ([args[@" javascriptChannelNames" ] isKindOfClass: [NSArray class ]]) {
84
84
NSArray * javaScriptChannelNames = args[@" javascriptChannelNames" ];
85
85
[_javaScriptChannelNames addObjectsFromArray: javaScriptChannelNames];
86
86
[self registerJavaScriptChannels: _javaScriptChannelNames controller: userContentController];
87
87
}
88
-
88
+
89
89
NSDictionary <NSString *, id >* settings = args[@" settings" ];
90
-
90
+
91
91
WKWebViewConfiguration * configuration = [[WKWebViewConfiguration alloc ] init ];
92
92
[self applyConfigurationSettings: settings toConfiguration: configuration];
93
93
configuration.userContentController = userContentController;
94
94
[self updateAutoMediaPlaybackPolicy: args[@" autoMediaPlaybackPolicy" ]
95
95
inConfiguration: configuration];
96
-
96
+
97
97
_webView = [[FLTWKWebView alloc ] initWithFrame: frame configuration: configuration];
98
98
_navigationDelegate = [[FLTWKNavigationDelegate alloc ] initWithChannel: _channel];
99
99
_webView.UIDelegate = self;
@@ -102,18 +102,18 @@ - (instancetype)initWithFrame:(CGRect)frame
102
102
[_channel setMethodCallHandler: ^(FlutterMethodCall* call, FlutterResult result) {
103
103
[weakSelf onMethodCall: call result: result];
104
104
}];
105
-
105
+
106
106
if (@available (iOS 11.0 , *)) {
107
107
_webView.scrollView .contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
108
108
if (@available (iOS 13.0 , *)) {
109
109
_webView.scrollView .automaticallyAdjustsScrollIndicatorInsets = NO ;
110
110
}
111
111
}
112
-
112
+
113
113
[self applySettings: settings];
114
114
// TODO(amirh): return an error if apply settings failed once it's possible to do so.
115
115
// https://github.com/flutter/flutter/issues/36228
116
-
116
+
117
117
NSString * initialUrl = args[@" initialUrl" ];
118
118
if ([initialUrl isKindOfClass: [NSString class ]]) {
119
119
[self loadUrl: initialUrl];
@@ -188,9 +188,9 @@ - (void)onUpdateSettings:(FlutterMethodCall*)call result:(FlutterResult)result {
188
188
- (void )onLoadUrl : (FlutterMethodCall*)call result : (FlutterResult)result {
189
189
if (![self loadRequest: [call arguments ]]) {
190
190
result ([FlutterError
191
- errorWithCode: @" loadUrl_failed"
192
- message: @" Failed parsing the URL"
193
- details: [NSString stringWithFormat: @" Request was: '%@ '" , [call arguments ]]]);
191
+ errorWithCode: @" loadUrl_failed"
192
+ message: @" Failed parsing the URL"
193
+ details: [NSString stringWithFormat: @" Request was: '%@ '" , [call arguments ]]]);
194
194
} else {
195
195
result (nil );
196
196
}
@@ -236,16 +236,16 @@ - (void)onEvaluateJavaScript:(FlutterMethodCall*)call result:(FlutterResult)resu
236
236
}
237
237
[_webView evaluateJavaScript: jsString
238
238
completionHandler: ^(_Nullable id evaluateResult, NSError * _Nullable error) {
239
- if (error) {
240
- result ([FlutterError
241
- errorWithCode: @" evaluateJavaScript_failed"
242
- message: @" Failed evaluating JavaScript"
243
- details: [NSString stringWithFormat: @" JavaScript string was: '%@ '\n %@ " ,
244
- jsString, error]]);
245
- } else {
246
- result ([NSString stringWithFormat: @" %@ " , evaluateResult]);
247
- }
248
- }];
239
+ if (error) {
240
+ result ([FlutterError
241
+ errorWithCode: @" evaluateJavaScript_failed"
242
+ message: @" Failed evaluating JavaScript"
243
+ details: [NSString stringWithFormat: @" JavaScript string was: '%@ '\n %@ " ,
244
+ jsString, error]]);
245
+ } else {
246
+ result ([NSString stringWithFormat: @" %@ " , evaluateResult]);
247
+ }
248
+ }];
249
249
}
250
250
251
251
- (void )onRunJavaScript : (FlutterMethodCall*)call
@@ -259,27 +259,28 @@ - (void)onRunJavaScript:(FlutterMethodCall*)call
259
259
return ;
260
260
}
261
261
[_webView
262
- evaluateJavaScript: jsString
263
- completionHandler: ^(_Nullable id evaluateResult, NSError * _Nullable error) {
264
- if (error) {
265
- // WebKit will throw an error (WKErrorJavaScriptResultTypeIsUnsupported) when the
266
- // type of the evaluated value is unsupported. This also goes for
267
- // `null` and `undefined` on iOS 14+, for example when running a void function.
268
- // For ease of use this specific error is ignored when no return value is expected.
269
- BOOL sendError = sendReturnValue || error.code != WKErrorJavaScriptResultTypeIsUnsupported ;
270
- result (
271
- sendError
272
- ? [FlutterError
273
- errorWithCode: (sendReturnValue ? @" runJavascriptReturningResult_failed"
274
- : @" runJavascript_failed" )
275
- message: @" Failed running JavaScript"
276
- details: [NSString stringWithFormat: @" JavaScript string was: '%@ '\n %@ " ,
277
- jsString, error]]
278
- : nil );
279
- return ;
280
- }
281
- result (sendReturnValue ? [NSString stringWithFormat: @" %@ " , evaluateResult] : nil );
282
- }];
262
+ evaluateJavaScript: jsString
263
+ completionHandler: ^(_Nullable id evaluateResult, NSError * _Nullable error) {
264
+ if (error) {
265
+ // WebKit will throw an error (WKErrorJavaScriptResultTypeIsUnsupported) when the
266
+ // type of the evaluated value is unsupported. This also goes for
267
+ // `null` and `undefined` on iOS 14+, for example when running a void function.
268
+ // For ease of use this specific error is ignored when no return value is expected.
269
+ BOOL sendError =
270
+ sendReturnValue || error.code != WKErrorJavaScriptResultTypeIsUnsupported ;
271
+ result (sendError
272
+ ? [FlutterError
273
+ errorWithCode: (sendReturnValue ? @" runJavascriptReturningResult_failed"
274
+ : @" runJavascript_failed" )
275
+ message: @" Failed running JavaScript"
276
+ details: [NSString
277
+ stringWithFormat: @" JavaScript string was: '%@ '\n %@ " ,
278
+ jsString, error]]
279
+ : nil );
280
+ return ;
281
+ }
282
+ result (sendReturnValue ? [NSString stringWithFormat: @" %@ " , evaluateResult] : nil );
283
+ }];
283
284
}
284
285
285
286
- (void )onAddJavaScriptChannels : (FlutterMethodCall*)call result : (FlutterResult)result {
@@ -298,12 +299,12 @@ - (void)onRemoveJavaScriptChannels:(FlutterMethodCall*)call result:(FlutterResul
298
299
for (NSString * channelName in _javaScriptChannelNames) {
299
300
[_webView.configuration.userContentController removeScriptMessageHandlerForName: channelName];
300
301
}
301
-
302
+
302
303
NSArray * channelNamesToRemove = [call arguments ];
303
304
for (NSString * channelName in channelNamesToRemove) {
304
305
[_javaScriptChannelNames removeObject: channelName];
305
306
}
306
-
307
+
307
308
[self registerJavaScriptChannels: _javaScriptChannelNames
308
309
controller: _webView.configuration.userContentController];
309
310
result (nil );
@@ -317,8 +318,8 @@ - (void)clearCache:(FlutterResult)result {
317
318
[dataStore removeDataOfTypes: cacheDataTypes
318
319
modifiedSince: dateFrom
319
320
completionHandler: ^{
320
- result (nil );
321
- }];
321
+ result (nil );
322
+ }];
322
323
} else {
323
324
// support for iOS8 tracked in https://github.com/flutter/flutter/issues/27624.
324
325
NSLog (@" Clearing cache is not supported for Flutter WebViews prior to iOS 9." );
@@ -334,18 +335,18 @@ - (void)onScrollTo:(FlutterMethodCall*)call result:(FlutterResult)result {
334
335
NSDictionary * arguments = [call arguments ];
335
336
int x = [arguments[@" x" ] intValue ];
336
337
int y = [arguments[@" y" ] intValue ];
337
-
338
+
338
339
_webView.scrollView .contentOffset = CGPointMake (x, y);
339
340
result (nil );
340
341
}
341
342
342
343
- (void )onScrollBy : (FlutterMethodCall*)call result : (FlutterResult)result {
343
344
CGPoint contentOffset = _webView.scrollView .contentOffset ;
344
-
345
+
345
346
NSDictionary * arguments = [call arguments ];
346
347
int x = [arguments[@" x" ] intValue ] + contentOffset.x ;
347
348
int y = [arguments[@" y" ] intValue ] + contentOffset.y ;
348
-
349
+
349
350
_webView.scrollView .contentOffset = CGPointMake (x, y);
350
351
result (nil );
351
352
}
@@ -382,7 +383,7 @@ - (NSString*)applySettings:(NSDictionary<NSString*, id>*)settings {
382
383
} else if ([key isEqualToString: @" gestureNavigationEnabled" ]) {
383
384
NSNumber * allowsBackForwardNavigationGestures = settings[key];
384
385
_webView.allowsBackForwardNavigationGestures =
385
- [allowsBackForwardNavigationGestures boolValue ];
386
+ [allowsBackForwardNavigationGestures boolValue ];
386
387
} else if ([key isEqualToString: @" userAgent" ]) {
387
388
NSString * userAgent = settings[key];
388
389
[self updateUserAgent: [userAgent isEqual: [NSNull null ]] ? nil : userAgent];
@@ -397,7 +398,7 @@ - (NSString*)applySettings:(NSDictionary<NSString*, id>*)settings {
397
398
return nil ;
398
399
}
399
400
return [NSString stringWithFormat: @" webview_flutter: unknown setting keys: {%@ }" ,
400
- [unknownKeys componentsJoinedByString: @" , " ]];
401
+ [unknownKeys componentsJoinedByString: @" , " ]];
401
402
}
402
403
403
404
- (void )applyConfigurationSettings : (NSDictionary <NSString*, id>*)settings
@@ -462,7 +463,7 @@ - (bool)loadRequest:(NSDictionary<NSString*, id>*)request {
462
463
if (!request) {
463
464
return false ;
464
465
}
465
-
466
+
466
467
NSString * url = request[@" url" ];
467
468
if ([url isKindOfClass: [NSString class ]]) {
468
469
id headers = request[@" headers" ];
@@ -472,7 +473,7 @@ - (bool)loadRequest:(NSDictionary<NSString*, id>*)request {
472
473
return [self loadUrl: url];
473
474
}
474
475
}
475
-
476
+
476
477
return false ;
477
478
}
478
479
@@ -495,15 +496,15 @@ - (void)registerJavaScriptChannels:(NSSet*)channelNames
495
496
controller : (WKUserContentController *)userContentController {
496
497
for (NSString * channelName in channelNames) {
497
498
FLTJavaScriptChannel* channel =
498
- [[FLTJavaScriptChannel alloc ] initWithMethodChannel: _channel
499
- javaScriptChannelName: channelName];
499
+ [[FLTJavaScriptChannel alloc ] initWithMethodChannel: _channel
500
+ javaScriptChannelName: channelName];
500
501
[userContentController addScriptMessageHandler: channel name: channelName];
501
502
NSString * wrapperSource = [NSString
502
- stringWithFormat: @" window.%@ = webkit.messageHandlers.%@ ;" , channelName, channelName];
503
+ stringWithFormat: @" window.%@ = webkit.messageHandlers.%@ ;" , channelName, channelName];
503
504
WKUserScript * wrapperScript =
504
- [[WKUserScript alloc ] initWithSource: wrapperSource
505
- injectionTime: WKUserScriptInjectionTimeAtDocumentStart
506
- forMainFrameOnly: NO ];
505
+ [[WKUserScript alloc ] initWithSource: wrapperSource
506
+ injectionTime: WKUserScriptInjectionTimeAtDocumentStart
507
+ forMainFrameOnly: NO ];
507
508
[userContentController addUserScript: wrapperScript];
508
509
}
509
510
}
@@ -519,13 +520,13 @@ - (void)updateUserAgent:(NSString*)userAgent {
519
520
#pragma mark WKUIDelegate
520
521
521
522
- (WKWebView *)webView : (WKWebView *)webView
522
- createWebViewWithConfiguration : (WKWebViewConfiguration *)configuration
523
- forNavigationAction : (WKNavigationAction *)navigationAction
524
- windowFeatures : (WKWindowFeatures *)windowFeatures {
523
+ createWebViewWithConfiguration : (WKWebViewConfiguration *)configuration
524
+ forNavigationAction : (WKNavigationAction *)navigationAction
525
+ windowFeatures : (WKWindowFeatures *)windowFeatures {
525
526
if (!navigationAction.targetFrame .isMainFrame ) {
526
527
[webView loadRequest: navigationAction.request];
527
528
}
528
-
529
+
529
530
return nil ;
530
531
}
531
532
0 commit comments