Skip to content

Commit

Permalink
Merge pull request #513 from charafau/reload_webview_headers
Browse files Browse the repository at this point in the history
Reload webview headers, ios clear cookies
  • Loading branch information
charafau authored Aug 15, 2019
2 parents 24abc82 + cd0437e commit 9feff62
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,13 @@ private void reload(MethodCall call, MethodChannel.Result result) {
private void reloadUrl(MethodCall call, MethodChannel.Result result) {
if (webViewManager != null) {
String url = call.argument("url");
webViewManager.reloadUrl(url);
Map<String, String> headers = call.argument("headers");
if (headers != null) {
webViewManager.reloadUrl(url, headers);
} else {
webViewManager.reloadUrl(url);
}

}
result.success(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,10 @@ void openUrl(
webView.getSettings().setUseWideViewPort(useWideViewPort);

// Handle debugging
webView.setWebContentsDebuggingEnabled(debuggingEnabled);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webView.setWebContentsDebuggingEnabled(debuggingEnabled);
}

webViewClient.updateInvalidUrlRegex(invalidUrlRegex);

if (geolocationEnabled) {
Expand Down Expand Up @@ -416,6 +418,10 @@ void reloadUrl(String url) {
webView.loadUrl(url);
}

void reloadUrl(String url, Map<String, String> headers) {
webView.loadUrl(url, headers);
}

void close(MethodCall call, MethodChannel.Result result) {
if (webView != null) {
ViewGroup vg = (ViewGroup) (webView.getParent());
Expand Down
22 changes: 19 additions & 3 deletions ios/Classes/FlutterWebviewPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,18 @@ - (void)initWebview:(FlutterMethodCall*)call {
}

if (clearCookies != (id)[NSNull null] && [clearCookies boolValue]) {
[[NSURLSession sharedSession] resetWithCompletionHandler:^{
}];
if (@available(iOS 9.0, *)) {
NSSet *websiteDataTypes
= [NSSet setWithArray:@[
WKWebsiteDataTypeCookies,
]];
NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];

[[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
}];
} else {
// Fallback on earlier versions
}
}

if (userAgent != (id)[NSNull null]) {
Expand Down Expand Up @@ -217,7 +227,13 @@ - (void)closeWebView {
- (void)reloadUrl:(FlutterMethodCall*)call {
if (self.webview != nil) {
NSString *url = call.arguments[@"url"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
NSDictionary *headers = call.arguments[@"headers"];

if (headers != nil) {
[request setAllHTTPHeaderFields:headers];
}

[self.webview loadRequest:request];
}
}
Expand Down
7 changes: 5 additions & 2 deletions lib/src/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,11 @@ class FlutterWebviewPlugin {
Future<Null> show() async => await _channel.invokeMethod('show');

// Reload webview with a url
Future<Null> reloadUrl(String url) async {
final args = <String, String>{'url': url};
Future<Null> reloadUrl(String url, {Map<String, String> headers}) async {
final args = <String, dynamic>{'url': url};
if (headers != null) {
args['headers'] = headers;
}
await _channel.invokeMethod('reloadUrl', args);
}

Expand Down

0 comments on commit 9feff62

Please sign in to comment.