Skip to content

Commit fee1fee

Browse files
authored
Compile fix ios (#603)
* Fixed ios compilation, added clear cache for ios * added cleaning cache for android * updated readme
1 parent bc1d7af commit fee1fee

File tree

8 files changed

+83
-20
lines changed

8 files changed

+83
-20
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.3.9+1
2+
3+
- Fixed error methods on iOS
4+
15
# 0.3.9
26

37
- Fixed error methods on iOS

android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.content.Intent;
77
import android.graphics.Point;
88
import android.view.Display;
9+
import android.webkit.WebStorage;
910
import android.widget.FrameLayout;
1011
import android.webkit.CookieManager;
1112
import android.webkit.ValueCallback;
@@ -90,12 +91,21 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
9091
case "canGoForward":
9192
canGoForward(result);
9293
break;
94+
case "cleanCache":
95+
cleanCache(result);
96+
break;
9397
default:
9498
result.notImplemented();
9599
break;
96100
}
97101
}
98102

103+
private void cleanCache(MethodChannel.Result result) {
104+
webViewManager.cleanCache();
105+
WebStorage.getInstance().deleteAllData();
106+
result.success(null);
107+
}
108+
99109
void openUrl(MethodCall call, MethodChannel.Result result) {
100110
boolean hidden = call.argument("hidden");
101111
String url = call.argument("url");

android/src/main/java/com/flutter_webview_plugin/WebviewManager.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,13 @@ boolean canGoForward() {
509509
return webView.canGoForward();
510510
}
511511

512+
/**
513+
* Clears cache
514+
*/
515+
void cleanCache(){
516+
webView.clearCache(true);
517+
}
518+
512519
void hide(MethodCall call, MethodChannel.Result result) {
513520
if (webView != null) {
514521
webView.setVisibility(View.GONE);

ios/Classes/FlutterWebviewPlugin.m

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#import "FlutterWebviewPlugin.h"
2-
#import "JavaScriptChannelHandler.h"
2+
#import "WebviewJavaScriptChannelHandler.h"
33

44
static 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

ios/Classes/JavaScriptChannelHandler.h renamed to ios/Classes/WebviewJavaScriptChannelHandler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
NS_ASSUME_NONNULL_BEGIN
99

10-
@interface FLTJavaScriptChannel : NSObject <WKScriptMessageHandler>
10+
@interface FLTCommunityJavaScriptChannel : NSObject <WKScriptMessageHandler>
1111

1212
- (instancetype)initWithMethodChannel:(FlutterMethodChannel*)methodChannel
1313
javaScriptChannelName:(NSString*)javaScriptChannelName;

ios/Classes/JavaScriptChannelHandler.m renamed to ios/Classes/WebviewJavaScriptChannelHandler.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
#import "JavaScriptChannelHandler.h"
5+
#import "WebviewJavaScriptChannelHandler.h"
66

7-
@implementation FLTJavaScriptChannel {
7+
@implementation FLTCommunityJavaScriptChannel {
88
FlutterMethodChannel* _methodChannel;
99
NSString* _javaScriptChannelName;
1010
}

lib/src/base.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,9 @@ class FlutterWebviewPlugin {
254254
// Shows the webview
255255
Future<Null> show() async => await _channel.invokeMethod('show');
256256

257+
// Clears browser cache
258+
Future<Null> clearCache() async => await _channel.invokeMethod('cleanCache');
259+
257260
// Reload webview with a url
258261
Future<Null> reloadUrl(String url, {Map<String, String> headers}) async {
259262
final args = <String, dynamic>{'url': url};

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ authors:
77
- Simon Lightfoot <[email protected]>
88
- Rafal Wachol <[email protected]>
99
homepage: https://github.com/dart-flitter/flutter_webview_plugin
10-
version: 0.3.9
10+
version: 0.3.9+1
1111
maintainer: Rafal Wachol (@RafalWachol)
1212

1313
environment:

0 commit comments

Comments
 (0)