1
1
#import " FlutterWebviewPlugin.h"
2
- #import " JavaScriptChannelHandler .h"
2
+ #import " WebviewJavaScriptChannelHandler .h"
3
3
4
4
static NSString *const CHANNEL_NAME = @" flutter_webview_plugin" ;
5
5
@@ -20,7 +20,7 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
20
20
21
21
UIViewController *viewController = [UIApplication sharedApplication ].delegate .window .rootViewController ;
22
22
FlutterWebviewPlugin* instance = [[FlutterWebviewPlugin alloc ] initWithViewController: viewController];
23
-
23
+
24
24
[registrar addMethodCallDelegate: instance channel: channel];
25
25
}
26
26
@@ -35,7 +35,7 @@ - (instancetype)initWithViewController:(UIViewController *)viewController {
35
35
- (void )handleMethodCall : (FlutterMethodCall*)call result : (FlutterResult)result {
36
36
if ([@" launch" isEqualToString: call.method]) {
37
37
if (!self.webview )
38
- [self initWebview: call];
38
+ [self initWebview: call withResult: result ];
39
39
else
40
40
[self navigate: call];
41
41
result (nil );
@@ -62,8 +62,7 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
62
62
[self stopLoading ];
63
63
result (nil );
64
64
} else if ([@" cleanCookies" isEqualToString: call.method]) {
65
- [self cleanCookies ];
66
- result (nil );
65
+ [self cleanCookies: result];
67
66
} else if ([@" back" isEqualToString: call.method]) {
68
67
[self back ];
69
68
result (nil );
@@ -77,12 +76,14 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
77
76
[self onCanGoBack: call result: result];
78
77
} else if ([@" canGoForward" isEqualToString: call.method]) {
79
78
[self onCanGoForward: call result: result];
79
+ } else if ([@" cleanCache" isEqualToString: call.method]) {
80
+ [self cleanCache: result];
80
81
} else {
81
82
result (FlutterMethodNotImplemented);
82
83
}
83
84
}
84
85
85
- - (void )initWebview : (FlutterMethodCall*)call {
86
+ - (void )initWebview : (FlutterMethodCall*)call withResult : (FlutterResult) result {
86
87
NSNumber *clearCache = call.arguments [@" clearCache" ];
87
88
NSNumber *clearCookies = call.arguments [@" clearCookies" ];
88
89
NSNumber *hidden = call.arguments [@" hidden" ];
@@ -105,6 +106,8 @@ - (void)initWebview:(FlutterMethodCall*)call {
105
106
106
107
if (clearCache != (id )[NSNull null ] && [clearCache boolValue ]) {
107
108
[[NSURLCache sharedURLCache ] removeAllCachedResponses ];
109
+ [self cleanCache: result];
110
+
108
111
}
109
112
110
113
if (clearCookies != (id )[NSNull null ] && [clearCookies boolValue ]) {
@@ -113,6 +116,9 @@ - (void)initWebview:(FlutterMethodCall*)call {
113
116
{
114
117
[storage deleteCookie: cookie];
115
118
}
119
+
120
+ [self cleanCookies: result];
121
+
116
122
}
117
123
118
124
if (userAgent != (id )[NSNull null ]) {
@@ -256,12 +262,45 @@ - (void)reloadUrl:(FlutterMethodCall*)call {
256
262
}
257
263
}
258
264
259
- - (void )cleanCookies {
265
+ - (void )cleanCookies : (FlutterResult) result {
260
266
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." );
265
304
}
266
305
}
267
306
}
@@ -311,7 +350,7 @@ - (void)reload {
311
350
312
351
- (bool )checkInvalidUrl : (NSURL *)url {
313
352
NSString * urlString = url != nil ? [url absoluteString ] : nil ;
314
- if (_invalidUrlRegex != [NSNull null ] && urlString != nil ) {
353
+ if (![ _invalidUrlRegex isEqual: [NSNull null ] ] && urlString != nil ) {
315
354
NSError * error = NULL ;
316
355
NSRegularExpression * regex =
317
356
[NSRegularExpression regularExpressionWithPattern: _invalidUrlRegex
@@ -331,10 +370,10 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigati
331
370
decisionHandler : (void (^)(WKNavigationActionPolicy ))decisionHandler {
332
371
333
372
BOOL isInvalid = [self checkInvalidUrl: navigationAction.request.URL];
334
-
373
+
335
374
id data = @{@" url" : navigationAction.request .URL .absoluteString ,
336
375
@" type" : isInvalid ? @" abortLoad" : @" shouldStart" ,
337
- @" navigationType" : [NSNumber numberWithInt : navigationAction.navigationType]};
376
+ @" navigationType" : [NSNumber numberWithInteger : navigationAction.navigationType]};
338
377
[channel invokeMethod: @" onState" arguments: data];
339
378
340
379
if (navigationAction.navigationType == WKNavigationTypeBackForward ) {
@@ -401,8 +440,8 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNaviga
401
440
- (void )registerJavaScriptChannels : (NSSet *)channelNames
402
441
controller : (WKUserContentController *)userContentController {
403
442
for (NSString * channelName in channelNames) {
404
- FLTJavaScriptChannel * _channel =
405
- [[FLTJavaScriptChannel alloc ] initWithMethodChannel: channel
443
+ FLTCommunityJavaScriptChannel * _channel =
444
+ [[FLTCommunityJavaScriptChannel alloc ] initWithMethodChannel: channel
406
445
javaScriptChannelName: channelName];
407
446
[userContentController addScriptMessageHandler: _channel name: channelName];
408
447
NSString * wrapperSource = [NSString
0 commit comments