@@ -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,27 @@ - (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 = 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
+ }];
283
283
}
284
284
285
285
- (void )onAddJavaScriptChannels : (FlutterMethodCall*)call result : (FlutterResult)result {
@@ -298,12 +298,12 @@ - (void)onRemoveJavaScriptChannels:(FlutterMethodCall*)call result:(FlutterResul
298
298
for (NSString * channelName in _javaScriptChannelNames) {
299
299
[_webView.configuration.userContentController removeScriptMessageHandlerForName: channelName];
300
300
}
301
-
301
+
302
302
NSArray * channelNamesToRemove = [call arguments ];
303
303
for (NSString * channelName in channelNamesToRemove) {
304
304
[_javaScriptChannelNames removeObject: channelName];
305
305
}
306
-
306
+
307
307
[self registerJavaScriptChannels: _javaScriptChannelNames
308
308
controller: _webView.configuration.userContentController];
309
309
result (nil );
@@ -317,8 +317,8 @@ - (void)clearCache:(FlutterResult)result {
317
317
[dataStore removeDataOfTypes: cacheDataTypes
318
318
modifiedSince: dateFrom
319
319
completionHandler: ^{
320
- result (nil );
321
- }];
320
+ result (nil );
321
+ }];
322
322
} else {
323
323
// support for iOS8 tracked in https://github.com/flutter/flutter/issues/27624.
324
324
NSLog (@" Clearing cache is not supported for Flutter WebViews prior to iOS 9." );
@@ -334,18 +334,18 @@ - (void)onScrollTo:(FlutterMethodCall*)call result:(FlutterResult)result {
334
334
NSDictionary * arguments = [call arguments ];
335
335
int x = [arguments[@" x" ] intValue ];
336
336
int y = [arguments[@" y" ] intValue ];
337
-
337
+
338
338
_webView.scrollView .contentOffset = CGPointMake (x, y);
339
339
result (nil );
340
340
}
341
341
342
342
- (void )onScrollBy : (FlutterMethodCall*)call result : (FlutterResult)result {
343
343
CGPoint contentOffset = _webView.scrollView .contentOffset ;
344
-
344
+
345
345
NSDictionary * arguments = [call arguments ];
346
346
int x = [arguments[@" x" ] intValue ] + contentOffset.x ;
347
347
int y = [arguments[@" y" ] intValue ] + contentOffset.y ;
348
-
348
+
349
349
_webView.scrollView .contentOffset = CGPointMake (x, y);
350
350
result (nil );
351
351
}
@@ -382,7 +382,7 @@ - (NSString*)applySettings:(NSDictionary<NSString*, id>*)settings {
382
382
} else if ([key isEqualToString: @" gestureNavigationEnabled" ]) {
383
383
NSNumber * allowsBackForwardNavigationGestures = settings[key];
384
384
_webView.allowsBackForwardNavigationGestures =
385
- [allowsBackForwardNavigationGestures boolValue ];
385
+ [allowsBackForwardNavigationGestures boolValue ];
386
386
} else if ([key isEqualToString: @" userAgent" ]) {
387
387
NSString * userAgent = settings[key];
388
388
[self updateUserAgent: [userAgent isEqual: [NSNull null ]] ? nil : userAgent];
@@ -397,7 +397,7 @@ - (NSString*)applySettings:(NSDictionary<NSString*, id>*)settings {
397
397
return nil ;
398
398
}
399
399
return [NSString stringWithFormat: @" webview_flutter: unknown setting keys: {%@ }" ,
400
- [unknownKeys componentsJoinedByString: @" , " ]];
400
+ [unknownKeys componentsJoinedByString: @" , " ]];
401
401
}
402
402
403
403
- (void )applyConfigurationSettings : (NSDictionary <NSString*, id>*)settings
@@ -462,7 +462,7 @@ - (bool)loadRequest:(NSDictionary<NSString*, id>*)request {
462
462
if (!request) {
463
463
return false ;
464
464
}
465
-
465
+
466
466
NSString * url = request[@" url" ];
467
467
if ([url isKindOfClass: [NSString class ]]) {
468
468
id headers = request[@" headers" ];
@@ -472,7 +472,7 @@ - (bool)loadRequest:(NSDictionary<NSString*, id>*)request {
472
472
return [self loadUrl: url];
473
473
}
474
474
}
475
-
475
+
476
476
return false ;
477
477
}
478
478
@@ -495,15 +495,15 @@ - (void)registerJavaScriptChannels:(NSSet*)channelNames
495
495
controller : (WKUserContentController *)userContentController {
496
496
for (NSString * channelName in channelNames) {
497
497
FLTJavaScriptChannel* channel =
498
- [[FLTJavaScriptChannel alloc ] initWithMethodChannel: _channel
499
- javaScriptChannelName: channelName];
498
+ [[FLTJavaScriptChannel alloc ] initWithMethodChannel: _channel
499
+ javaScriptChannelName: channelName];
500
500
[userContentController addScriptMessageHandler: channel name: channelName];
501
501
NSString * wrapperSource = [NSString
502
- stringWithFormat: @" window.%@ = webkit.messageHandlers.%@ ;" , channelName, channelName];
502
+ stringWithFormat: @" window.%@ = webkit.messageHandlers.%@ ;" , channelName, channelName];
503
503
WKUserScript * wrapperScript =
504
- [[WKUserScript alloc ] initWithSource: wrapperSource
505
- injectionTime: WKUserScriptInjectionTimeAtDocumentStart
506
- forMainFrameOnly: NO ];
504
+ [[WKUserScript alloc ] initWithSource: wrapperSource
505
+ injectionTime: WKUserScriptInjectionTimeAtDocumentStart
506
+ forMainFrameOnly: NO ];
507
507
[userContentController addUserScript: wrapperScript];
508
508
}
509
509
}
@@ -519,13 +519,13 @@ - (void)updateUserAgent:(NSString*)userAgent {
519
519
#pragma mark WKUIDelegate
520
520
521
521
- (WKWebView *)webView : (WKWebView *)webView
522
- createWebViewWithConfiguration : (WKWebViewConfiguration *)configuration
523
- forNavigationAction : (WKNavigationAction *)navigationAction
524
- windowFeatures : (WKWindowFeatures *)windowFeatures {
522
+ createWebViewWithConfiguration : (WKWebViewConfiguration *)configuration
523
+ forNavigationAction : (WKNavigationAction *)navigationAction
524
+ windowFeatures : (WKWindowFeatures *)windowFeatures {
525
525
if (!navigationAction.targetFrame .isMainFrame ) {
526
526
[webView loadRequest: navigationAction.request];
527
527
}
528
-
528
+
529
529
return nil ;
530
530
}
531
531
0 commit comments