Skip to content

Fix clear cache and cookies on iOS. #228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
case "back":
back(call, result);
break;
case "canGoBack":
canGoBack(call, result);
break;
case "forward":
forward(call, result);
break;
Expand Down Expand Up @@ -175,6 +178,13 @@ private void back(MethodCall call, MethodChannel.Result result) {
result.success(null);
}

private void canGoBack(MethodCall call, MethodChannel.Result result) {
if (webViewManager != null) {
result.success(webViewManager.canGoBack());
}
result.success(null);
}

/**
* Navigates forward on the Webview.
*/
Expand Down
24 changes: 21 additions & 3 deletions ios/Classes/FlutterWebviewPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
} else if ([@"back" isEqualToString:call.method]) {
[self back];
result(nil);
} else if ([@"canGoBack" isEqualToString:call.method]) {
result([NSNumber numberWithBool:self.webview.canGoBack]);
} else if ([@"forward" isEqualToString:call.method]) {
[self forward];
result(nil);
Expand All @@ -88,12 +90,28 @@ - (void)initWebview:(FlutterMethodCall*)call {
_invalidUrlRegex = call.arguments[@"invalidUrlRegex"];

if (clearCache != (id)[NSNull null] && [clearCache boolValue]) {
[[NSURLCache sharedURLCache] removeAllCachedResponses];
if (@available(iOS 11.3, *)) {
NSSet *dataTypes = [NSSet setWithArray:@[WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache, WKWebsiteDataTypeFetchCache]];
[[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:dataTypes modifiedSince:[NSDate dateWithTimeIntervalSince1970:0] completionHandler:^(){
}];
} else if (@available(iOS 9.0, *)) {
NSSet *dataTypes = [NSSet setWithArray:@[WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache]];
[[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:dataTypes modifiedSince:[NSDate dateWithTimeIntervalSince1970:0] completionHandler:^(){
}];
} else {
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}
}

if (clearCookies != (id)[NSNull null] && [clearCookies boolValue]) {
[[NSURLSession sharedSession] resetWithCompletionHandler:^{
}];
if (@available(iOS 9.0, *)) {
NSSet *dataTypes = [NSSet setWithArray:@[WKWebsiteDataTypeCookies]];
[[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:dataTypes modifiedSince:[NSDate dateWithTimeIntervalSince1970:0] completionHandler:^(){
}];
} else {
[[NSURLSession sharedSession] resetWithCompletionHandler:^{
}];
}
}

if (userAgent != (id)[NSNull null]) {
Expand Down
2 changes: 2 additions & 0 deletions lib/src/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ class FlutterWebviewPlugin {
/// Navigates back on the Webview.
Future<Null> goBack() async => await _channel.invokeMethod('back');

Future<bool> canGoBack() async => await _channel.invokeMethod('canGoBack');

/// Navigates forward on the Webview.
Future<Null> goForward() async => await _channel.invokeMethod('forward');

Expand Down