Skip to content

Commit

Permalink
Add support for passing a list of cookies to be set
Browse files Browse the repository at this point in the history
  • Loading branch information
craigloftus committed Mar 22, 2019
1 parent 271e68b commit fc07e70
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.webkit.ValueCallback;
import android.os.Build;

import java.util.ArrayList;
import java.util.Map;

import io.flutter.plugin.common.MethodCall;
Expand Down Expand Up @@ -90,6 +91,7 @@ private void openUrl(MethodCall call, MethodChannel.Result result) {
boolean withJavascript = call.argument("withJavascript");
boolean clearCache = call.argument("clearCache");
boolean clearCookies = call.argument("clearCookies");
ArrayList<String> cookies = call.argument("cookies");
boolean withZoom = call.argument("withZoom");
boolean withLocalStorage = call.argument("withLocalStorage");
boolean supportMultipleWindows = call.argument("supportMultipleWindows");
Expand All @@ -111,6 +113,7 @@ private void openUrl(MethodCall call, MethodChannel.Result result) {
clearCache,
hidden,
clearCookies,
cookies,
userAgent,
url,
headers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -187,6 +188,12 @@ public void onReceiveValue(Boolean aBoolean) {
}
}

private void setCookies(String url, ArrayList<String> cookies) {
for (String cookie : cookies) {
CookieManager.getInstance().setCookie(url, cookie);
}
}

private void clearCache() {
webView.clearCache(true);
webView.clearFormData();
Expand All @@ -197,6 +204,7 @@ void openUrl(
boolean clearCache,
boolean hidden,
boolean clearCookies,
ArrayList<String> cookies,
String userAgent,
String url,
Map<String, String> headers,
Expand Down Expand Up @@ -247,6 +255,10 @@ public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermiss
clearCookies();
}

if (cookies != null && !cookies.isEmpty()) {
setCookies(url, cookies);
}

if (userAgent != null) {
webView.getSettings().setUserAgentString(userAgent);
}
Expand Down
45 changes: 35 additions & 10 deletions ios/Classes/FlutterWebviewPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,15 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
}

- (void)initWebview:(FlutterMethodCall*)call {
NSString *url = call.arguments[@"url"];
NSNumber *clearCache = call.arguments[@"clearCache"];
NSNumber *clearCookies = call.arguments[@"clearCookies"];
NSNumber *hidden = call.arguments[@"hidden"];
NSDictionary *rect = call.arguments[@"rect"];
_enableAppScheme = call.arguments[@"enableAppScheme"];
NSString *userAgent = call.arguments[@"userAgent"];
NSNumber *withZoom = call.arguments[@"withZoom"];
NSArray *cookies = call.arguments[@"cookies"];
NSNumber *scrollBar = call.arguments[@"scrollBar"];

if (clearCache != (id)[NSNull null] && [clearCache boolValue]) {
Expand All @@ -104,19 +106,42 @@ - (void)initWebview:(FlutterMethodCall*)call {
rc = self.viewController.view.bounds;
}

self.webview = [[WKWebView alloc] initWithFrame:rc];
self.webview.UIDelegate = self;
self.webview.navigationDelegate = self;
self.webview.scrollView.delegate = self;
self.webview.hidden = [hidden boolValue];
self.webview.scrollView.showsHorizontalScrollIndicator = [scrollBar boolValue];
self.webview.scrollView.showsVerticalScrollIndicator = [scrollBar boolValue];
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
WKWebsiteDataStore* store = [WKWebsiteDataStore nonPersistentDataStore];

_enableZoom = [withZoom boolValue];
dispatch_group_t group = dispatch_group_create();

[self.viewController.view addSubview:self.webview];
if (cookies != nil) {
NSURL* parsedUrl = [NSURL URLWithString:url];
NSString* cookieString = [cookies componentsJoinedByString: @", "];
NSDictionary* fakeHeaders = @{@"Set-Cookie": cookieString};
NSArray* cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:fakeHeaders forURL:parsedUrl];

[self navigate:call];
for(NSHTTPCookie *cookie in cookies) {
dispatch_group_enter(group);
[store.httpCookieStore setCookie:cookie completionHandler:^{
dispatch_group_leave(group);
}];
};
}

dispatch_group_notify(group, dispatch_get_main_queue(), ^{
config.websiteDataStore = store;

self.webview = [[WKWebView alloc] initWithFrame:rc configuration:config];
self.webview.UIDelegate = self;
self.webview.navigationDelegate = self;
self.webview.scrollView.delegate = self;
self.webview.hidden = [hidden boolValue];
self.webview.scrollView.showsHorizontalScrollIndicator = [scrollBar boolValue];
self.webview.scrollView.showsVerticalScrollIndicator = [scrollBar boolValue];

_enableZoom = [withZoom boolValue];

[self.viewController.view addSubview:self.webview];

[self navigate:call];
});
}

- (CGRect)parseRect:(NSDictionary *)rect {
Expand Down
7 changes: 7 additions & 0 deletions lib/src/base.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:io';
import 'dart:ui';

import 'package:flutter/material.dart';
Expand Down Expand Up @@ -82,6 +83,7 @@ class FlutterWebviewPlugin {
/// iOS WebView: Not implemented yet
/// - [clearCache] clear the cache of the Webview
/// - [clearCookies] clear all cookies of the Webview
/// - [cookies] An initial list of cookies to populate the Webview's cookiejar
/// - [hidden] not show
/// - [rect]: show in rect, fullscreen if null
/// - [enableAppScheme]: false will enable all schemes, true only for httt/https/about
Expand All @@ -99,6 +101,7 @@ class FlutterWebviewPlugin {
bool withJavascript,
bool clearCache,
bool clearCookies,
List<Cookie> cookies,
bool hidden,
bool enableAppScheme,
Rect rect,
Expand All @@ -112,12 +115,16 @@ class FlutterWebviewPlugin {
bool allowFileURLs,
bool geolocationEnabled,
}) async {

final List<String> serializedCookies = cookies.map((cookie) => cookie.toString()).toList();

final args = <String, dynamic>{
'url': url,
'withJavascript': withJavascript ?? true,
'clearCache': clearCache ?? false,
'hidden': hidden ?? false,
'clearCookies': clearCookies ?? false,
'cookies': serializedCookies,
'enableAppScheme': enableAppScheme ?? true,
'userAgent': userAgent,
'withZoom': withZoom ?? false,
Expand Down
4 changes: 4 additions & 0 deletions lib/src/webview_scaffold.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
Expand All @@ -16,6 +17,7 @@ class WebviewScaffold extends StatefulWidget {
this.withJavascript,
this.clearCache,
this.clearCookies,
this.cookies,
this.enableAppScheme,
this.userAgent,
this.primary = true,
Expand All @@ -39,6 +41,7 @@ class WebviewScaffold extends StatefulWidget {
final bool withJavascript;
final bool clearCache;
final bool clearCookies;
final List<Cookie> cookies;
final bool enableAppScheme;
final String userAgent;
final bool primary;
Expand Down Expand Up @@ -116,6 +119,7 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
withJavascript: widget.withJavascript,
clearCache: widget.clearCache,
clearCookies: widget.clearCookies,
cookies: widget.cookies,
hidden: widget.hidden,
enableAppScheme: widget.enableAppScheme,
userAgent: widget.userAgent,
Expand Down

0 comments on commit fc07e70

Please sign in to comment.