11#import " FlutterWebviewPlugin.h"
2- #import " JavaScriptChannelHandler .h"
2+ #import " WebviewJavaScriptChannelHandler .h"
33
44static NSString *const CHANNEL_NAME = @" flutter_webview_plugin" ;
55
@@ -20,7 +20,7 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
2020
2121 UIViewController *viewController = [UIApplication sharedApplication ].delegate .window .rootViewController ;
2222 FlutterWebviewPlugin* instance = [[FlutterWebviewPlugin alloc ] initWithViewController: viewController];
23-
23+
2424 [registrar addMethodCallDelegate: instance channel: channel];
2525}
2626
@@ -35,7 +35,7 @@ - (instancetype)initWithViewController:(UIViewController *)viewController {
3535- (void )handleMethodCall : (FlutterMethodCall*)call result : (FlutterResult)result {
3636 if ([@" launch" isEqualToString: call.method]) {
3737 if (!self.webview )
38- [self initWebview: call];
38+ [self initWebview: call withResult: result ];
3939 else
4040 [self navigate: call];
4141 result (nil );
@@ -62,8 +62,7 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
6262 [self stopLoading ];
6363 result (nil );
6464 } else if ([@" cleanCookies" isEqualToString: call.method]) {
65- [self cleanCookies ];
66- result (nil );
65+ [self cleanCookies: result];
6766 } else if ([@" back" isEqualToString: call.method]) {
6867 [self back ];
6968 result (nil );
@@ -77,12 +76,14 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
7776 [self onCanGoBack: call result: result];
7877 } else if ([@" canGoForward" isEqualToString: call.method]) {
7978 [self onCanGoForward: call result: result];
79+ } else if ([@" cleanCache" isEqualToString: call.method]) {
80+ [self cleanCache: result];
8081 } else {
8182 result (FlutterMethodNotImplemented);
8283 }
8384}
8485
85- - (void )initWebview : (FlutterMethodCall*)call {
86+ - (void )initWebview : (FlutterMethodCall*)call withResult : (FlutterResult) result {
8687 NSNumber *clearCache = call.arguments [@" clearCache" ];
8788 NSNumber *clearCookies = call.arguments [@" clearCookies" ];
8889 NSNumber *hidden = call.arguments [@" hidden" ];
@@ -105,6 +106,8 @@ - (void)initWebview:(FlutterMethodCall*)call {
105106
106107 if (clearCache != (id )[NSNull null ] && [clearCache boolValue ]) {
107108 [[NSURLCache sharedURLCache ] removeAllCachedResponses ];
109+ [self cleanCache: result];
110+
108111 }
109112
110113 if (clearCookies != (id )[NSNull null ] && [clearCookies boolValue ]) {
@@ -113,6 +116,9 @@ - (void)initWebview:(FlutterMethodCall*)call {
113116 {
114117 [storage deleteCookie: cookie];
115118 }
119+
120+ [self cleanCookies: result];
121+
116122 }
117123
118124 if (userAgent != (id )[NSNull null ]) {
@@ -256,12 +262,45 @@ - (void)reloadUrl:(FlutterMethodCall*)call {
256262 }
257263}
258264
259- - (void )cleanCookies {
265+ - (void )cleanCookies : (FlutterResult) result {
260266 if (self.webview != nil ) {
261- NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage ];
262- for (NSHTTPCookie *cookie in [storage cookies ])
263- {
264- [storage deleteCookie: cookie];
267+ [[NSURLSession sharedSession ] resetWithCompletionHandler: ^{
268+ }];
269+ if (@available (iOS 9.0 , *)) {
270+ NSSet <NSString *> *websiteDataTypes = [NSSet setWithObject: WKWebsiteDataTypeCookies ];
271+ WKWebsiteDataStore *dataStore = [WKWebsiteDataStore defaultDataStore ];
272+
273+ void (^deleteAndNotify)(NSArray <WKWebsiteDataRecord *> *) =
274+ ^(NSArray <WKWebsiteDataRecord *> *cookies) {
275+ [dataStore removeDataOfTypes: websiteDataTypes
276+ forDataRecords: cookies
277+ completionHandler: ^{
278+ result (nil );
279+ }];
280+ };
281+
282+ [dataStore fetchDataRecordsOfTypes: websiteDataTypes completionHandler: deleteAndNotify];
283+ } else {
284+ // support for iOS8 tracked in https://github.com/flutter/flutter/issues/27624.
285+ NSLog (@" Clearing cookies is not supported for Flutter WebViews prior to iOS 9." );
286+ }
287+ }
288+ }
289+
290+ - (void )cleanCache : (FlutterResult)result {
291+ if (self.webview != nil ) {
292+ if (@available (iOS 9.0 , *)) {
293+ NSSet * cacheDataTypes = [WKWebsiteDataStore allWebsiteDataTypes ];
294+ WKWebsiteDataStore * dataStore = [WKWebsiteDataStore defaultDataStore ];
295+ NSDate * dateFrom = [NSDate dateWithTimeIntervalSince1970: 0 ];
296+ [dataStore removeDataOfTypes: cacheDataTypes
297+ modifiedSince: dateFrom
298+ completionHandler: ^{
299+ result (nil );
300+ }];
301+ } else {
302+ // support for iOS8 tracked in https://github.com/flutter/flutter/issues/27624.
303+ NSLog (@" Clearing cache is not supported for Flutter WebViews prior to iOS 9." );
265304 }
266305 }
267306}
@@ -311,7 +350,7 @@ - (void)reload {
311350
312351- (bool )checkInvalidUrl : (NSURL *)url {
313352 NSString * urlString = url != nil ? [url absoluteString ] : nil ;
314- if (_invalidUrlRegex != [NSNull null ] && urlString != nil ) {
353+ if (![ _invalidUrlRegex isEqual: [NSNull null ] ] && urlString != nil ) {
315354 NSError * error = NULL ;
316355 NSRegularExpression * regex =
317356 [NSRegularExpression regularExpressionWithPattern: _invalidUrlRegex
@@ -331,10 +370,10 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigati
331370 decisionHandler : (void (^)(WKNavigationActionPolicy ))decisionHandler {
332371
333372 BOOL isInvalid = [self checkInvalidUrl: navigationAction.request.URL];
334-
373+
335374 id data = @{@" url" : navigationAction.request .URL .absoluteString ,
336375 @" type" : isInvalid ? @" abortLoad" : @" shouldStart" ,
337- @" navigationType" : [NSNumber numberWithInt : navigationAction.navigationType]};
376+ @" navigationType" : [NSNumber numberWithInteger : navigationAction.navigationType]};
338377 [channel invokeMethod: @" onState" arguments: data];
339378
340379 if (navigationAction.navigationType == WKNavigationTypeBackForward ) {
@@ -401,8 +440,8 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNaviga
401440- (void )registerJavaScriptChannels : (NSSet *)channelNames
402441 controller : (WKUserContentController *)userContentController {
403442 for (NSString * channelName in channelNames) {
404- FLTJavaScriptChannel * _channel =
405- [[FLTJavaScriptChannel alloc ] initWithMethodChannel: channel
443+ FLTCommunityJavaScriptChannel * _channel =
444+ [[FLTCommunityJavaScriptChannel alloc ] initWithMethodChannel: channel
406445 javaScriptChannelName: channelName];
407446 [userContentController addScriptMessageHandler: _channel name: channelName];
408447 NSString * wrapperSource = [NSString
0 commit comments