Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to set cookies to webview when loading url #110

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.view.Display;
import android.widget.FrameLayout;

import java.util.List;
import java.util.Map;

import io.flutter.plugin.common.MethodCall;
Expand Down Expand Up @@ -86,6 +87,7 @@ private void openUrl(MethodCall call, MethodChannel.Result result) {
boolean clearCookies = call.argument("clearCookies");
boolean withZoom = call.argument("withZoom");
boolean withLocalStorage = call.argument("withLocalStorage");
Map<String, String> cookies = call.argument("cookies");
Map<String, String> headers = call.argument("headers");
boolean scrollBar = call.argument("scrollBar");

Expand All @@ -106,6 +108,7 @@ private void openUrl(MethodCall call, MethodChannel.Result result) {
headers,
withZoom,
withLocalStorage,
cookies,
scrollBar
);
result.success(null);
Expand Down Expand Up @@ -181,6 +184,7 @@ private void reloadUrl(MethodCall call, MethodChannel.Result result) {
null,
false,
false,
null,
false
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import java.util.Map;

import static android.app.Activity.RESULT_OK;

Expand Down Expand Up @@ -184,12 +185,18 @@ public void onReceiveValue(Boolean aBoolean) {
}
}

private void setCookies(String url, Map<String, String> cookies) {
for (String key : cookies.keySet()) {
CookieManager.getInstance().setCookie(url, key + "=" + cookies.get(key));
}
}

private void clearCache() {
webView.clearCache(true);
webView.clearFormData();
}

void openUrl(boolean withJavascript, boolean clearCache, boolean hidden, boolean clearCookies, String userAgent, String url, Map<String, String> headers, boolean withZoom, boolean withLocalStorage, boolean scrollBar) {
void openUrl(boolean withJavascript, boolean clearCache, boolean hidden, boolean clearCookies, String userAgent, String url, Map<String, String> headers, boolean withZoom, boolean withLocalStorage, Map<String, String> cookies, boolean scrollBar) {
webView.getSettings().setJavaScriptEnabled(withJavascript);
webView.getSettings().setBuiltInZoomControls(withZoom);
webView.getSettings().setSupportZoom(withZoom);
Expand All @@ -207,6 +214,10 @@ void openUrl(boolean withJavascript, boolean clearCache, boolean hidden, boolean
clearCookies();
}

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

if (userAgent != null) {
webView.getSettings().setUserAgentString(userAgent);
}
Expand Down
3 changes: 3 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ class _MyHomePageState extends State<MyHomePage> {
new RaisedButton(
onPressed: () {
flutterWebviewPlugin.launch(selectedUrl,
cookies: {
"sessionid": "1234567890"
},
rect: new Rect.fromLTWH(
0.0, 0.0, MediaQuery.of(context).size.width, 300.0),
userAgent: kAndroidUserAgent);
Expand Down
22 changes: 22 additions & 0 deletions ios/Classes/FlutterWebviewPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,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"];
NSDictionary *cookies = call.arguments[@"cookies"];
NSNumber *scrollBar = call.arguments[@"scrollBar"];

if (clearCache != (id)[NSNull null] && [clearCache boolValue]) {
Expand All @@ -81,6 +83,26 @@ - (void)initWebview:(FlutterMethodCall*)call {
[[NSURLSession sharedSession] resetWithCompletionHandler:^{
}];
}

if (cookies != (id)[NSNull null]) {
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL* stop) {
NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:@key forKey:NSHTTPCookieName];
[cookieProperties setObject:@value forKey:NSHTTPCookieValue];
[cookieProperties setObject:@url forKey:NSHTTPCookieDomain];
[cookieProperties setObject:@url forKey:NSHTTPCookieOriginURL];
[cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
[cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];

// set expiration to one month from now or any NSDate of your choosing
// this makes the cookie sessionless and it will persist across web sessions and app launches
/// if you want the cookie to be destroyed when your app exits, don't set this
[cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];

NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}];
}

if (userAgent != (id)[NSNull null]) {
[[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent": userAgent}];
Expand Down
2 changes: 2 additions & 0 deletions lib/src/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class FlutterWebviewPlugin {
bool withJavascript,
bool clearCache,
bool clearCookies,
Map<String, String> cookies,
bool hidden,
bool enableAppScheme,
Rect rect,
Expand All @@ -114,6 +115,7 @@ class FlutterWebviewPlugin {
'clearCache': clearCache ?? false,
'hidden': hidden ?? false,
'clearCookies': clearCookies ?? false,
'cookies': cookies,
'enableAppScheme': enableAppScheme ?? true,
'userAgent': userAgent,
'withZoom': withZoom ?? false,
Expand Down
3 changes: 3 additions & 0 deletions lib/src/webview_scaffold.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class WebviewScaffold extends StatefulWidget {
final bool withJavascript;
final bool clearCache;
final bool clearCookies;
final Map<String, String> cookies;
final bool enableAppScheme;
final String userAgent;
final bool primary;
Expand All @@ -31,6 +32,7 @@ class WebviewScaffold extends StatefulWidget {
this.withJavascript,
this.clearCache,
this.clearCookies,
this.cookies,
this.enableAppScheme,
this.userAgent,
this.primary = true,
Expand Down Expand Up @@ -73,6 +75,7 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
withJavascript: widget.withJavascript,
clearCache: widget.clearCache,
clearCookies: widget.clearCookies,
cookies: widget.cookies,
enableAppScheme: widget.enableAppScheme,
userAgent: widget.userAgent,
rect: _rect,
Expand Down