@@ -74,26 +74,26 @@ - (instancetype)initWithFrame:(CGRect)frame
7474 binaryMessenger : (NSObject <FlutterBinaryMessenger>*)messenger {
7575 if (self = [super init ]) {
7676 _viewId = viewId;
77-
77+
7878 NSString * channelName = [NSString stringWithFormat: @" plugins.flutter.io/webview_%lld " , viewId];
7979 _channel = [FlutterMethodChannel methodChannelWithName: channelName binaryMessenger: messenger];
8080 _javaScriptChannelNames = [[NSMutableSet alloc ] init ];
81-
81+
8282 WKUserContentController * userContentController = [[WKUserContentController alloc ] init ];
8383 if ([args[@" javascriptChannelNames" ] isKindOfClass: [NSArray class ]]) {
8484 NSArray * javaScriptChannelNames = args[@" javascriptChannelNames" ];
8585 [_javaScriptChannelNames addObjectsFromArray: javaScriptChannelNames];
8686 [self registerJavaScriptChannels: _javaScriptChannelNames controller: userContentController];
8787 }
88-
88+
8989 NSDictionary <NSString *, id >* settings = args[@" settings" ];
90-
90+
9191 WKWebViewConfiguration * configuration = [[WKWebViewConfiguration alloc ] init ];
9292 [self applyConfigurationSettings: settings toConfiguration: configuration];
9393 configuration.userContentController = userContentController;
9494 [self updateAutoMediaPlaybackPolicy: args[@" autoMediaPlaybackPolicy" ]
9595 inConfiguration: configuration];
96-
96+
9797 _webView = [[FLTWKWebView alloc ] initWithFrame: frame configuration: configuration];
9898 _navigationDelegate = [[FLTWKNavigationDelegate alloc ] initWithChannel: _channel];
9999 _webView.UIDelegate = self;
@@ -102,18 +102,18 @@ - (instancetype)initWithFrame:(CGRect)frame
102102 [_channel setMethodCallHandler: ^(FlutterMethodCall* call, FlutterResult result) {
103103 [weakSelf onMethodCall: call result: result];
104104 }];
105-
105+
106106 if (@available (iOS 11.0 , *)) {
107107 _webView.scrollView .contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
108108 if (@available (iOS 13.0 , *)) {
109109 _webView.scrollView .automaticallyAdjustsScrollIndicatorInsets = NO ;
110110 }
111111 }
112-
112+
113113 [self applySettings: settings];
114114 // TODO(amirh): return an error if apply settings failed once it's possible to do so.
115115 // https://github.com/flutter/flutter/issues/36228
116-
116+
117117 NSString * initialUrl = args[@" initialUrl" ];
118118 if ([initialUrl isKindOfClass: [NSString class ]]) {
119119 [self loadUrl: initialUrl];
@@ -188,9 +188,9 @@ - (void)onUpdateSettings:(FlutterMethodCall*)call result:(FlutterResult)result {
188188- (void )onLoadUrl : (FlutterMethodCall*)call result : (FlutterResult)result {
189189 if (![self loadRequest: [call arguments ]]) {
190190 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 ]]]);
194194 } else {
195195 result (nil );
196196 }
@@ -236,16 +236,16 @@ - (void)onEvaluateJavaScript:(FlutterMethodCall*)call result:(FlutterResult)resu
236236 }
237237 [_webView evaluateJavaScript: jsString
238238 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+ }];
249249}
250250
251251- (void )onRunJavaScript : (FlutterMethodCall*)call
@@ -259,27 +259,28 @@ - (void)onRunJavaScript:(FlutterMethodCall*)call
259259 return ;
260260 }
261261 [_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+ }];
283284}
284285
285286- (void )onAddJavaScriptChannels : (FlutterMethodCall*)call result : (FlutterResult)result {
@@ -298,12 +299,12 @@ - (void)onRemoveJavaScriptChannels:(FlutterMethodCall*)call result:(FlutterResul
298299 for (NSString * channelName in _javaScriptChannelNames) {
299300 [_webView.configuration.userContentController removeScriptMessageHandlerForName: channelName];
300301 }
301-
302+
302303 NSArray * channelNamesToRemove = [call arguments ];
303304 for (NSString * channelName in channelNamesToRemove) {
304305 [_javaScriptChannelNames removeObject: channelName];
305306 }
306-
307+
307308 [self registerJavaScriptChannels: _javaScriptChannelNames
308309 controller: _webView.configuration.userContentController];
309310 result (nil );
@@ -317,8 +318,8 @@ - (void)clearCache:(FlutterResult)result {
317318 [dataStore removeDataOfTypes: cacheDataTypes
318319 modifiedSince: dateFrom
319320 completionHandler: ^{
320- result (nil );
321- }];
321+ result (nil );
322+ }];
322323 } else {
323324 // support for iOS8 tracked in https://github.com/flutter/flutter/issues/27624.
324325 NSLog (@" Clearing cache is not supported for Flutter WebViews prior to iOS 9." );
@@ -334,18 +335,18 @@ - (void)onScrollTo:(FlutterMethodCall*)call result:(FlutterResult)result {
334335 NSDictionary * arguments = [call arguments ];
335336 int x = [arguments[@" x" ] intValue ];
336337 int y = [arguments[@" y" ] intValue ];
337-
338+
338339 _webView.scrollView .contentOffset = CGPointMake (x, y);
339340 result (nil );
340341}
341342
342343- (void )onScrollBy : (FlutterMethodCall*)call result : (FlutterResult)result {
343344 CGPoint contentOffset = _webView.scrollView .contentOffset ;
344-
345+
345346 NSDictionary * arguments = [call arguments ];
346347 int x = [arguments[@" x" ] intValue ] + contentOffset.x ;
347348 int y = [arguments[@" y" ] intValue ] + contentOffset.y ;
348-
349+
349350 _webView.scrollView .contentOffset = CGPointMake (x, y);
350351 result (nil );
351352}
@@ -382,7 +383,7 @@ - (NSString*)applySettings:(NSDictionary<NSString*, id>*)settings {
382383 } else if ([key isEqualToString: @" gestureNavigationEnabled" ]) {
383384 NSNumber * allowsBackForwardNavigationGestures = settings[key];
384385 _webView.allowsBackForwardNavigationGestures =
385- [allowsBackForwardNavigationGestures boolValue ];
386+ [allowsBackForwardNavigationGestures boolValue ];
386387 } else if ([key isEqualToString: @" userAgent" ]) {
387388 NSString * userAgent = settings[key];
388389 [self updateUserAgent: [userAgent isEqual: [NSNull null ]] ? nil : userAgent];
@@ -397,7 +398,7 @@ - (NSString*)applySettings:(NSDictionary<NSString*, id>*)settings {
397398 return nil ;
398399 }
399400 return [NSString stringWithFormat: @" webview_flutter: unknown setting keys: {%@ }" ,
400- [unknownKeys componentsJoinedByString: @" , " ]];
401+ [unknownKeys componentsJoinedByString: @" , " ]];
401402}
402403
403404- (void )applyConfigurationSettings : (NSDictionary <NSString*, id>*)settings
@@ -462,7 +463,7 @@ - (bool)loadRequest:(NSDictionary<NSString*, id>*)request {
462463 if (!request) {
463464 return false ;
464465 }
465-
466+
466467 NSString * url = request[@" url" ];
467468 if ([url isKindOfClass: [NSString class ]]) {
468469 id headers = request[@" headers" ];
@@ -472,7 +473,7 @@ - (bool)loadRequest:(NSDictionary<NSString*, id>*)request {
472473 return [self loadUrl: url];
473474 }
474475 }
475-
476+
476477 return false ;
477478}
478479
@@ -495,15 +496,15 @@ - (void)registerJavaScriptChannels:(NSSet*)channelNames
495496 controller : (WKUserContentController *)userContentController {
496497 for (NSString * channelName in channelNames) {
497498 FLTJavaScriptChannel* channel =
498- [[FLTJavaScriptChannel alloc ] initWithMethodChannel: _channel
499- javaScriptChannelName: channelName];
499+ [[FLTJavaScriptChannel alloc ] initWithMethodChannel: _channel
500+ javaScriptChannelName: channelName];
500501 [userContentController addScriptMessageHandler: channel name: channelName];
501502 NSString * wrapperSource = [NSString
502- stringWithFormat: @" window.%@ = webkit.messageHandlers.%@ ;" , channelName, channelName];
503+ stringWithFormat: @" window.%@ = webkit.messageHandlers.%@ ;" , channelName, channelName];
503504 WKUserScript * wrapperScript =
504- [[WKUserScript alloc ] initWithSource: wrapperSource
505- injectionTime: WKUserScriptInjectionTimeAtDocumentStart
506- forMainFrameOnly: NO ];
505+ [[WKUserScript alloc ] initWithSource: wrapperSource
506+ injectionTime: WKUserScriptInjectionTimeAtDocumentStart
507+ forMainFrameOnly: NO ];
507508 [userContentController addUserScript: wrapperScript];
508509 }
509510}
@@ -519,13 +520,13 @@ - (void)updateUserAgent:(NSString*)userAgent {
519520#pragma mark WKUIDelegate
520521
521522- (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 {
525526 if (!navigationAction.targetFrame .isMainFrame ) {
526527 [webView loadRequest: navigationAction.request];
527528 }
528-
529+
529530 return nil ;
530531}
531532
0 commit comments