Skip to content

Commit

Permalink
Compile fix ios (#603)
Browse files Browse the repository at this point in the history
* Fixed ios compilation, added clear cache for ios

* added cleaning cache for android

* updated readme
  • Loading branch information
charafau committed Nov 15, 2019
1 parent 9eef77a commit 70f571d
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.3.9+1

- Fixed error methods on iOS

# 0.3.9

- Fixed error methods on iOS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.content.Intent;
import android.graphics.Point;
import android.view.Display;
import android.webkit.WebStorage;
import android.widget.FrameLayout;
import android.webkit.CookieManager;
import android.webkit.ValueCallback;
Expand Down Expand Up @@ -90,12 +91,21 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
case "canGoForward":
canGoForward(result);
break;
case "cleanCache":
cleanCache(result);
break;
default:
result.notImplemented();
break;
}
}

private void cleanCache(MethodChannel.Result result) {
webViewManager.cleanCache();
WebStorage.getInstance().deleteAllData();
result.success(null);
}

void openUrl(MethodCall call, MethodChannel.Result result) {
boolean hidden = call.argument("hidden");
String url = call.argument("url");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,13 @@ boolean canGoForward() {
return webView.canGoForward();
}

/**
* Clears cache
*/
void cleanCache(){
webView.clearCache(true);
}

void hide(MethodCall call, MethodChannel.Result result) {
if (webView != null) {
webView.setVisibility(View.GONE);
Expand Down
71 changes: 55 additions & 16 deletions ios/Classes/FlutterWebviewPlugin.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#import "FlutterWebviewPlugin.h"
#import "JavaScriptChannelHandler.h"
#import "WebviewJavaScriptChannelHandler.h"

static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";

Expand All @@ -20,7 +20,7 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {

UIViewController *viewController = [UIApplication sharedApplication].delegate.window.rootViewController;
FlutterWebviewPlugin* instance = [[FlutterWebviewPlugin alloc] initWithViewController:viewController];

[registrar addMethodCallDelegate:instance channel:channel];
}

Expand All @@ -35,7 +35,7 @@ - (instancetype)initWithViewController:(UIViewController *)viewController {
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([@"launch" isEqualToString:call.method]) {
if (!self.webview)
[self initWebview:call];
[self initWebview:call withResult:result];
else
[self navigate:call];
result(nil);
Expand All @@ -62,8 +62,7 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
[self stopLoading];
result(nil);
} else if ([@"cleanCookies" isEqualToString:call.method]) {
[self cleanCookies];
result(nil);
[self cleanCookies:result];
} else if ([@"back" isEqualToString:call.method]) {
[self back];
result(nil);
Expand All @@ -77,12 +76,14 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
[self onCanGoBack:call result:result];
} else if ([@"canGoForward" isEqualToString:call.method]) {
[self onCanGoForward:call result:result];
} else if ([@"cleanCache" isEqualToString:call.method]) {
[self cleanCache:result];
} else {
result(FlutterMethodNotImplemented);
}
}

- (void)initWebview:(FlutterMethodCall*)call {
- (void)initWebview:(FlutterMethodCall*)call withResult:(FlutterResult)result {
NSNumber *clearCache = call.arguments[@"clearCache"];
NSNumber *clearCookies = call.arguments[@"clearCookies"];
NSNumber *hidden = call.arguments[@"hidden"];
Expand All @@ -105,6 +106,8 @@ - (void)initWebview:(FlutterMethodCall*)call {

if (clearCache != (id)[NSNull null] && [clearCache boolValue]) {
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[self cleanCache:result];

}

if (clearCookies != (id)[NSNull null] && [clearCookies boolValue]) {
Expand All @@ -113,6 +116,9 @@ - (void)initWebview:(FlutterMethodCall*)call {
{
[storage deleteCookie:cookie];
}

[self cleanCookies:result];

}

if (userAgent != (id)[NSNull null]) {
Expand Down Expand Up @@ -256,12 +262,45 @@ - (void)reloadUrl:(FlutterMethodCall*)call {
}
}

- (void)cleanCookies {
- (void)cleanCookies:(FlutterResult)result {
if(self.webview != nil) {
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [storage cookies])
{
[storage deleteCookie:cookie];
[[NSURLSession sharedSession] resetWithCompletionHandler:^{
}];
if (@available(iOS 9.0, *)) {
NSSet<NSString *> *websiteDataTypes = [NSSet setWithObject:WKWebsiteDataTypeCookies];
WKWebsiteDataStore *dataStore = [WKWebsiteDataStore defaultDataStore];

void (^deleteAndNotify)(NSArray<WKWebsiteDataRecord *> *) =
^(NSArray<WKWebsiteDataRecord *> *cookies) {
[dataStore removeDataOfTypes:websiteDataTypes
forDataRecords:cookies
completionHandler:^{
result(nil);
}];
};

[dataStore fetchDataRecordsOfTypes:websiteDataTypes completionHandler:deleteAndNotify];
} else {
// support for iOS8 tracked in https://github.com/flutter/flutter/issues/27624.
NSLog(@"Clearing cookies is not supported for Flutter WebViews prior to iOS 9.");
}
}
}

- (void)cleanCache:(FlutterResult)result {
if (self.webview != nil) {
if (@available(iOS 9.0, *)) {
NSSet* cacheDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
WKWebsiteDataStore* dataStore = [WKWebsiteDataStore defaultDataStore];
NSDate* dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
[dataStore removeDataOfTypes:cacheDataTypes
modifiedSince:dateFrom
completionHandler:^{
result(nil);
}];
} else {
// support for iOS8 tracked in https://github.com/flutter/flutter/issues/27624.
NSLog(@"Clearing cache is not supported for Flutter WebViews prior to iOS 9.");
}
}
}
Expand Down Expand Up @@ -311,7 +350,7 @@ - (void)reload {

- (bool)checkInvalidUrl:(NSURL*)url {
NSString* urlString = url != nil ? [url absoluteString] : nil;
if (_invalidUrlRegex != [NSNull null] && urlString != nil) {
if (![_invalidUrlRegex isEqual:[NSNull null]] && urlString != nil) {
NSError* error = NULL;
NSRegularExpression* regex =
[NSRegularExpression regularExpressionWithPattern:_invalidUrlRegex
Expand All @@ -331,10 +370,10 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigati
decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {

BOOL isInvalid = [self checkInvalidUrl: navigationAction.request.URL];

id data = @{@"url": navigationAction.request.URL.absoluteString,
@"type": isInvalid ? @"abortLoad" : @"shouldStart",
@"navigationType": [NSNumber numberWithInt:navigationAction.navigationType]};
@"navigationType": [NSNumber numberWithInteger:navigationAction.navigationType]};
[channel invokeMethod:@"onState" arguments:data];

if (navigationAction.navigationType == WKNavigationTypeBackForward) {
Expand Down Expand Up @@ -401,8 +440,8 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNaviga
- (void)registerJavaScriptChannels:(NSSet*)channelNames
controller:(WKUserContentController*)userContentController {
for (NSString* channelName in channelNames) {
FLTJavaScriptChannel* _channel =
[[FLTJavaScriptChannel alloc] initWithMethodChannel: channel
FLTCommunityJavaScriptChannel* _channel =
[[FLTCommunityJavaScriptChannel alloc] initWithMethodChannel: channel
javaScriptChannelName:channelName];
[userContentController addScriptMessageHandler:_channel name:channelName];
NSString* wrapperSource = [NSString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

NS_ASSUME_NONNULL_BEGIN

@interface FLTJavaScriptChannel : NSObject <WKScriptMessageHandler>
@interface FLTCommunityJavaScriptChannel : NSObject <WKScriptMessageHandler>

- (instancetype)initWithMethodChannel:(FlutterMethodChannel*)methodChannel
javaScriptChannelName:(NSString*)javaScriptChannelName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "JavaScriptChannelHandler.h"
#import "WebviewJavaScriptChannelHandler.h"

@implementation FLTJavaScriptChannel {
@implementation FLTCommunityJavaScriptChannel {
FlutterMethodChannel* _methodChannel;
NSString* _javaScriptChannelName;
}
Expand Down
3 changes: 3 additions & 0 deletions lib/src/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ class FlutterWebviewPlugin {
// Shows the webview
Future<Null> show() async => await _channel.invokeMethod('show');

// Clears browser cache
Future<Null> clearCache() async => await _channel.invokeMethod('cleanCache');

// Reload webview with a url
Future<Null> reloadUrl(String url, {Map<String, String> headers}) async {
final args = <String, dynamic>{'url': url};
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ authors:
- Simon Lightfoot <simon@devangels.london>
- Rafal Wachol <gorudonu@gmail.com>
homepage: https://github.com/dart-flitter/flutter_webview_plugin
version: 0.3.9
version: 0.3.9+1
maintainer: Rafal Wachol (@RafalWachol)

environment:
Expand Down

0 comments on commit 70f571d

Please sign in to comment.