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 Nov 19, 2019
1 parent 70f571d commit 73f43a7
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ 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 displayZoomControls = call.argument("displayZoomControls");
boolean withLocalStorage = call.argument("withLocalStorage");
Expand Down Expand Up @@ -144,6 +145,7 @@ 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 @@ -24,8 +24,8 @@
import android.database.Cursor;
import android.provider.OpenableColumns;

import java.util.List;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.io.File;
Expand Down Expand Up @@ -334,6 +334,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 @@ -351,6 +357,7 @@ void openUrl(
boolean clearCache,
boolean hidden,
boolean clearCookies,
ArrayList<String> cookies,
String userAgent,
String url,
Map<String, String> headers,
Expand Down Expand Up @@ -411,6 +418,10 @@ void openUrl(
clearCookies();
}

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

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

- (void)initWebview:(FlutterMethodCall*)call withResult:(FlutterResult)result {
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"];
NSNumber *withJavascript = call.arguments[@"withJavascript"];
_invalidUrlRegex = call.arguments[@"invalidUrlRegex"];
Expand Down Expand Up @@ -134,30 +136,52 @@ - (void)initWebview:(FlutterMethodCall*)call withResult:(FlutterResult)result {

WKWebViewConfiguration* configuration = [[WKWebViewConfiguration alloc] init];
configuration.userContentController = userContentController;
self.webview = [[WKWebView alloc] initWithFrame:rc configuration:configuration];
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];
WKWebsiteDataStore* store = [WKWebsiteDataStore nonPersistentDataStore];

dispatch_group_t group = dispatch_group_create();

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

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(), ^{
configuration.websiteDataStore = store;

self.webview = [[WKWebView alloc] initWithFrame:rc configuration:configuration];
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];

[self.webview addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];
[self.webview addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];

WKPreferences* preferences = [[self.webview configuration] preferences];
if ([withJavascript boolValue]) {
[preferences setJavaScriptEnabled:YES];
} else {
[preferences setJavaScriptEnabled:NO];
}
WKPreferences* preferences = [[self.webview configuration] preferences];
if ([withJavascript boolValue]) {
[preferences setJavaScriptEnabled:YES];
} else {
[preferences setJavaScriptEnabled:NO];
}

_enableZoom = [withZoom boolValue];
_enableZoom = [withZoom boolValue];

UIViewController* presentedViewController = self.viewController.presentedViewController;
UIViewController* currentViewController = presentedViewController != nil ? presentedViewController : self.viewController;
[currentViewController.view addSubview:self.webview];
UIViewController* presentedViewController = self.viewController.presentedViewController;
UIViewController* currentViewController = presentedViewController != nil ? presentedViewController : self.viewController;
[currentViewController.view addSubview:self.webview];

[self navigate:call];
[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 @@ -116,6 +117,7 @@ class FlutterWebviewPlugin {
/// - [withJavascript] enable Javascript or not for the Webview
/// - [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 Down Expand Up @@ -147,6 +149,7 @@ class FlutterWebviewPlugin {
bool withJavascript,
bool clearCache,
bool clearCookies,
List<Cookie> cookies,
bool hidden,
bool enableAppScheme,
Rect rect,
Expand All @@ -166,12 +169,16 @@ class FlutterWebviewPlugin {
bool geolocationEnabled,
bool debuggingEnabled,
}) 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 @@ -17,6 +18,7 @@ class WebviewScaffold extends StatefulWidget {
this.withJavascript,
this.clearCache,
this.clearCookies,
this.cookies,
this.enableAppScheme,
this.userAgent,
this.primary = true,
Expand Down Expand Up @@ -48,6 +50,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 @@ -156,6 +159,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 73f43a7

Please sign in to comment.