completed CookieManager class #8 #3

This commit is contained in:
pichillilorenzo 2018-10-27 21:07:00 +02:00
parent 81a4b6ad7f
commit ac962a59d7
6 changed files with 373 additions and 86 deletions

View File

@ -15,8 +15,11 @@
</component>
<component name="ChangeListManager">
<list default="true" id="9b41f7a2-a71e-4923-91fb-249d7815b3e7" name="Default" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/MyCookieManager.java" beforeDir="false" afterPath="$PROJECT_DIR$/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/MyCookieManager.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/ios/Classes/InAppBrowserWebViewController.swift" beforeDir="false" afterPath="$PROJECT_DIR$/ios/Classes/InAppBrowserWebViewController.swift" afterDir="false" />
<change beforePath="$PROJECT_DIR$/example/lib/main.dart" beforeDir="false" afterPath="$PROJECT_DIR$/example/lib/main.dart" afterDir="false" />
<change beforePath="$PROJECT_DIR$/ios/Classes/SwiftFlutterPlugin.swift" beforeDir="false" afterPath="$PROJECT_DIR$/ios/Classes/SwiftFlutterPlugin.swift" afterDir="false" />
<change beforePath="$PROJECT_DIR$/lib/flutter_inappbrowser.dart" beforeDir="false" afterPath="$PROJECT_DIR$/lib/flutter_inappbrowser.dart" afterDir="false" />
</list>
<ignored path="$PROJECT_DIR$/.dart_tool/" />
<ignored path="$PROJECT_DIR$/.idea/" />
@ -33,11 +36,11 @@
</component>
<component name="FileEditorManager">
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
<file leaf-file-name="flutter_inappbrowser.dart" pinned="false" current-in-tab="true">
<file leaf-file-name="flutter_inappbrowser.dart" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/lib/flutter_inappbrowser.dart">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="269">
<caret line="1179" column="42" lean-forward="true" selection-start-line="1179" selection-start-column="42" selection-end-line="1179" selection-end-column="42" />
<state relative-caret-position="8878">
<caret line="1144" column="23" selection-start-line="1144" selection-start-column="23" selection-end-line="1144" selection-end-column="23" />
<folding>
<element signature="e#814#831#0" expanded="true" />
</folding>
@ -45,11 +48,11 @@
</provider>
</entry>
</file>
<file leaf-file-name="main.dart" pinned="false" current-in-tab="false">
<file leaf-file-name="main.dart" pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/example/lib/main.dart">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="422">
<caret line="72" column="67" selection-start-line="72" selection-start-column="67" selection-end-line="72" selection-end-column="67" />
<state relative-caret-position="226">
<caret line="294" lean-forward="true" selection-start-line="294" selection-end-line="294" />
<folding>
<element signature="e#0#20#0" expanded="true" />
</folding>
@ -68,8 +71,6 @@
</component>
<component name="FindInProjectRecents">
<findStrings>
<find>onProgress</find>
<find>goBack</find>
<find>_controller</find>
<find>canGoBack</find>
<find>getOp</find>
@ -98,6 +99,8 @@
<find>headers</find>
<find>.cast</find>
<find>WebView</find>
<find>print</find>
<find>throw</find>
</findStrings>
<replaceStrings>
<replace>activity.getPreferences(0)</replace>
@ -142,16 +145,16 @@
<option value="$PROJECT_DIR$/README.md" />
<option value="$PROJECT_DIR$/pubspec.yaml" />
<option value="$PROJECT_DIR$/CHANGELOG.md" />
<option value="$PROJECT_DIR$/example/lib/main.dart" />
<option value="$PROJECT_DIR$/lib/flutter_inappbrowser.dart" />
<option value="$PROJECT_DIR$/example/lib/main.dart" />
</list>
</option>
</component>
<component name="ProjectFrameBounds">
<option name="x" value="133" />
<option name="x" value="1921" />
<option name="y" value="23" />
<option name="width" value="1787" />
<option name="height" value="1057" />
<option name="width" value="1749" />
<option name="height" value="1027" />
</component>
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectView">
@ -159,7 +162,7 @@
<foldersAlwaysOnTop value="true" />
</navigator>
<panes>
<pane id="PackagesPane" />
<pane id="AndroidView" />
<pane id="ProjectPane">
<subPane>
<expand>
@ -177,7 +180,6 @@
</subPane>
<option name="show-excluded-files" value="false" />
</pane>
<pane id="AndroidView" />
<pane id="Scope">
<subPane subId="Project Files">
<expand>
@ -189,6 +191,7 @@
<select />
</subPane>
</pane>
<pane id="PackagesPane" />
</panes>
</component>
<component name="PropertiesComponent">
@ -345,32 +348,33 @@
<servers />
</component>
<component name="ToolWindowManager">
<frame x="133" y="23" width="1787" height="1057" extended-state="0" />
<frame x="1921" y="23" width="1749" height="1027" extended-state="0" />
<editor active="true" />
<layout>
<window_info anchor="bottom" id="Android Profiler" order="7" show_stripe_button="false" />
<window_info anchor="bottom" id="TODO" order="6" />
<window_info anchor="bottom" id="Messages" order="12" weight="0.23594266" />
<window_info anchor="right" id="Palette&#9;" order="9" />
<window_info id="Image Layers" order="7" />
<window_info id="Build Variants" order="3" side_tool="true" />
<window_info anchor="right" id="Capture Analysis" order="4" />
<window_info anchor="bottom" id="Event Log" order="8" sideWeight="0.5035553" side_tool="true" weight="0.25689086" />
<window_info anchor="bottom" id="Event Log" order="8" sideWeight="0.5043937" side_tool="true" weight="0.30650496" />
<window_info anchor="bottom" id="Dart Analysis" order="14" weight="0.3276414" />
<window_info anchor="bottom" id="Run" order="2" sideWeight="0.49644473" weight="0.44503736" />
<window_info anchor="bottom" id="Run" order="2" sideWeight="0.49644473" weight="0.34068358" />
<window_info anchor="bottom" id="Version Control" order="9" />
<window_info active="true" anchor="bottom" id="Terminal" order="10" sideWeight="0.49644473" visible="true" weight="0.30736393" />
<window_info anchor="bottom" id="Terminal" order="10" sideWeight="0.49560633" visible="true" weight="0.30650496" />
<window_info anchor="right" id="Flutter Outline" order="6" weight="0.32922077" />
<window_info anchor="bottom" id="Logcat" order="11" />
<window_info id="Captures" order="4" weight="0.32936507" />
<window_info id="Capture Tool" order="6" />
<window_info id="Designer" order="2" />
<window_info content_ui="combo" id="Project" order="0" sideWeight="0.49724367" visible="true" weight="0.14670487" />
<window_info active="true" content_ui="combo" id="Project" order="0" sideWeight="0.49724367" visible="true" weight="0.14059754" />
<window_info id="Structure" order="1" sideWeight="0.50275636" side_tool="true" weight="0.1910871" />
<window_info anchor="right" id="Device File Explorer" order="5" side_tool="true" />
<window_info anchor="right" id="Theme Preview" order="7" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.34364995" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.34288865" />
<window_info id="Favorites" order="5" side_tool="true" />
<window_info anchor="right" id="Flutter Inspector" order="3" weight="0.32938388" />
<window_info anchor="bottom" id="Messages" order="12" weight="0.23692636" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
<window_info anchor="right" id="Commander" order="0" weight="0.4" />
<window_info anchor="right" id="Assistant" order="8" visible="true" weight="0.3295129" />
@ -633,13 +637,7 @@
</entry>
<entry file="file://$PROJECT_DIR$/README.md">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-3761">
<folding>
<element signature="e#0#20#0" expanded="true" />
<element signature="e#0#39#0" expanded="true" />
<element signature="e#0#39#0" expanded="true" />
</folding>
</state>
<state relative-caret-position="-3761" />
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/CHANGELOG.md">
@ -647,22 +645,22 @@
<state />
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/example/lib/main.dart">
<entry file="file://$PROJECT_DIR$/lib/flutter_inappbrowser.dart">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="422">
<caret line="72" column="67" selection-start-line="72" selection-start-column="67" selection-end-line="72" selection-end-column="67" />
<state relative-caret-position="8878">
<caret line="1144" column="23" selection-start-line="1144" selection-start-column="23" selection-end-line="1144" selection-end-column="23" />
<folding>
<element signature="e#0#20#0" expanded="true" />
<element signature="e#814#831#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/lib/flutter_inappbrowser.dart">
<entry file="file://$PROJECT_DIR$/example/lib/main.dart">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="269">
<caret line="1179" column="42" lean-forward="true" selection-start-line="1179" selection-start-column="42" selection-end-line="1179" selection-end-column="42" />
<state relative-caret-position="226">
<caret line="294" lean-forward="true" selection-start-line="294" selection-end-line="294" />
<folding>
<element signature="e#814#831#0" expanded="true" />
<element signature="e#0#20#0" expanded="true" />
</folding>
</state>
</provider>

View File

@ -6,6 +6,8 @@ import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.ValueCallback;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
@ -24,11 +26,13 @@ public class MyCookieManager implements MethodChannel.MethodCallHandler {
public static PluginRegistry.Registrar registrar;
public static MethodChannel channel;
public static CookieManager cookieManager;
public MyCookieManager(PluginRegistry.Registrar r) {
registrar = r;
channel = new MethodChannel(registrar.messenger(), "com.pichillilorenzo/flutter_inappbrowser_cookiemanager");
channel.setMethodCallHandler(this);
cookieManager = CookieManager.getInstance();
}
@Override
@ -41,10 +45,10 @@ public class MyCookieManager implements MethodChannel.MethodCallHandler {
String value = (String) call.argument("value");
String domain = (String) call.argument("domain");
String path = (String) call.argument("path");
Long expiresDate = new Long((Integer) call.argument("expiresDate"));
Boolean isHTTPOnly = (Boolean) call.argument("isHTTPOnly");
Long expiresDate = new Long((String) call.argument("expiresDate"));
Integer maxAge = (Integer) call.argument("maxAge");
Boolean isSecure = (Boolean) call.argument("isSecure");
MyCookieManager.setCookie(url, name, value, domain, path, expiresDate, isHTTPOnly, isSecure, result);
MyCookieManager.setCookie(url, name, value, domain, path, expiresDate, maxAge, isSecure, result);
}
break;
case "getCookies":
@ -54,11 +58,21 @@ public class MyCookieManager implements MethodChannel.MethodCallHandler {
{
String url = (String) call.argument("url");
String name = (String) call.argument("name");
MyCookieManager.deleteCookie(url, name, result);
String domain = (String) call.argument("domain");
String path = (String) call.argument("path");
MyCookieManager.deleteCookie(url, name, domain, path, result);
}
break;
case "deleteCookies":
MyCookieManager.deleteCookies(result);
{
String url = (String) call.argument("url");
String domain = (String) call.argument("domain");
String path = (String) call.argument("path");
MyCookieManager.deleteCookies(url, domain, path, result);
}
break;
case "deleteAllCookies":
MyCookieManager.deleteAllCookies(result);
break;
default:
result.notImplemented();
@ -71,28 +85,22 @@ public class MyCookieManager implements MethodChannel.MethodCallHandler {
String domain,
String path,
Long expiresDate,
Boolean isHTTPOnly,
Integer maxAge,
Boolean isSecure,
final MethodChannel.Result result) {
String cookieValue = name + "=" + value;
if (domain != null && !domain.isEmpty())
cookieValue += "; Domain=" + domain;
if (path != null && !path.isEmpty())
cookieValue += "; Path=" + path;
String cookieValue = name + "=" + value + "; Domain=" + domain + "; Path=" + path;
if (expiresDate != null)
cookieValue += "; Expires=" + getCookieExpirationDate(expiresDate);
if (isHTTPOnly != null && isHTTPOnly)
cookieValue += "; HttpOnly";
if (maxAge != null)
cookieValue += "; Max-Age=" + maxAge.toString();
if (isSecure != null && isSecure)
cookieValue += "; Secure";
CookieManager cookieManager = CookieManager.getInstance();
cookieValue += ";";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.setCookie(url, cookieValue, new ValueCallback<Boolean>() {
@ -117,27 +125,27 @@ public class MyCookieManager implements MethodChannel.MethodCallHandler {
final List<Map<String, Object>> cookieListMap = new ArrayList<>();
CookieManager cookieManager = CookieManager.getInstance();
String cookiesString = cookieManager.getCookie(url);
String[] cookies = cookieManager.getCookie(url).split(";");
for (String cookie : cookies) {
String[] nameValue = cookie.split("=", 2);
String name = nameValue[0].trim();
String value = nameValue[1].trim();
Map<String, Object> cookieMap = new HashMap<>();
cookieMap.put("name", name);
cookieMap.put("value", value);
cookieListMap.add(cookieMap);
if (cookiesString != null) {
String[] cookies = cookiesString.split(";");
for (String cookie : cookies) {
String[] nameValue = cookie.split("=", 2);
String name = nameValue[0].trim();
String value = nameValue[1].trim();
Map<String, Object> cookieMap = new HashMap<>();
cookieMap.put("name", name);
cookieMap.put("value", value);
cookieListMap.add(cookieMap);
}
}
return cookieListMap;
}
public static void deleteCookie(String url, String cookieName, final MethodChannel.Result result) {
public static void deleteCookie(String url, String name, String domain, String path, final MethodChannel.Result result) {
String cookieValue = cookieName + "=; Expires=Thu, 01 Jan 1970 00:00:01 GMT;";
CookieManager cookieManager = CookieManager.getInstance();
String cookieValue = name + "=; Path=" + path + "; Domain=" + domain + "; Max-Age=-1;";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.setCookie(url, cookieValue, new ValueCallback<Boolean>() {
@ -158,8 +166,40 @@ public class MyCookieManager implements MethodChannel.MethodCallHandler {
}
}
public static void deleteCookies(final MethodChannel.Result result) {
CookieManager cookieManager = CookieManager.getInstance();
public static void deleteCookies(String url, String domain, String path, final MethodChannel.Result result) {
CookieSyncManager cookieSyncMngr = null;
String cookiesString = cookieManager.getCookie(url);
if (cookiesString != null) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
cookieSyncMngr = CookieSyncManager.createInstance(registrar.context());
cookieSyncMngr.startSync();
}
String[] cookies = cookiesString.split(";");
for (String cookie : cookies) {
String[] nameValue = cookie.split("=", 2);
String name = nameValue[0].trim();
String cookieValue = name + "=; Path=" + path + "; Domain=" + domain + "; Max-Age=-1;";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
cookieManager.setCookie(url, cookieValue, null);
else
cookieManager.setCookie(url, cookieValue);
}
if (cookieSyncMngr != null) {
cookieSyncMngr.stopSync();
cookieSyncMngr.sync();
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
cookieManager.flush();
}
result.success(true);
}
public static void deleteAllCookies(final MethodChannel.Result result) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.removeAllCookies(new ValueCallback<Boolean>() {
@Override

View File

@ -65,13 +65,26 @@ class MyInAppBrowser extends InAppBrowser {
// await this.webViewController.injectScriptCode("console.error('testError', false);");
// await this.webViewController.injectScriptCode("console.debug('testDebug', true);");
//
print(await this.webViewController.injectScriptCode("document.cookie"));
print("");
print(await CookieManager.getCookies("https://flutter.io/"));
print("");
print(await CookieManager.getCookie("https://flutter.io/", "_ga"));
print("");
// print(await this.webViewController.injectScriptCode("document.cookie"));
//
// print("");
// print(await CookieManager.getCookies(url));
// print("");
// print(await CookieManager.getCookie(url, "my_cookie2"));
// print("");
// await CookieManager.deleteCookie(url, "my_cookie2");
// await CookieManager.deleteCookie(url, "_gid", domain: ".googleblog.com");
// print("");
// print(await CookieManager.getCookies(url));
// print("");
// await CookieManager.deleteCookies(url);
// print("");
// print(await CookieManager.getCookies(url));
// print("");
// await CookieManager.deleteAllCookies();
// print("");
// print(await CookieManager.getCookies(url));
// print("");
//
// print(await this.webViewController.injectScriptCode("null"));
// print(await this.webViewController.injectScriptCode("undefined"));
@ -277,7 +290,9 @@ class _MyAppState extends State<MyApp> {
// //"toolbarBottom": false
// });
//
await CookieManager.setCookie("https://flutter.io/", "my_cookie2", "cookieValue2", "flutter.io", expiresDate: 1000000, path: "/get-started/install");
// await CookieManager.setCookie("https://flutter.io/", "my_cookie2", "cookieValue2", domain: "flutter.io", expiresDate: 1540838864611);
// await CookieManager.setCookie("https://flutter.io/", "my_cookie", "cookieValue", domain: "flutter.io", expiresDate: 1540838864611);
await inAppBrowserFallback.open(url: "https://flutter.io/", options: {
//"useOnLoadResource": true,
//"hidden": true,

View File

@ -0,0 +1,167 @@
//
// MyCookieManager.swift
// flutter_inappbrowser
//
// Created by Lorenzo on 26/10/18.
//
import Foundation
import WebKit
@available(iOS 11.0, *)
class MyCookieManager: NSObject, FlutterPlugin {
static var registrar: FlutterPluginRegistrar?
static var channel: FlutterMethodChannel?
static var httpCookieStore: WKHTTPCookieStore?
static func register(with registrar: FlutterPluginRegistrar) {
}
init(registrar: FlutterPluginRegistrar) {
super.init()
MyCookieManager.registrar = registrar
MyCookieManager.httpCookieStore = WKWebsiteDataStore.default().httpCookieStore
MyCookieManager.channel = FlutterMethodChannel(name: "com.pichillilorenzo/flutter_inappbrowser_cookiemanager", binaryMessenger: registrar.messenger())
registrar.addMethodCallDelegate(self, channel: MyCookieManager.channel!)
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
let arguments = call.arguments as? NSDictionary
switch call.method {
case "setCookie":
let url = arguments!["url"] as! String
let name = arguments!["name"] as! String
let value = arguments!["value"] as! String
let domain = arguments!["domain"] as! String
let path = arguments!["path"] as! String
let expiresDate = arguments!["expiresDate"] as? Int
let maxAge = arguments!["maxAge"] as? Int
let isSecure = arguments!["isSecure"] as? Bool
MyCookieManager.setCookie(url: url, name: name, value: value, domain: domain, path: path, expiresDate: expiresDate, maxAge: maxAge, isSecure: isSecure, result: result)
break
case "getCookies":
let url = arguments!["url"] as! String
MyCookieManager.getCookies(url: url, result: result)
break
case "deleteCookie":
let url = arguments!["url"] as! String
let name = arguments!["name"] as! String
let domain = arguments!["domain"] as! String
let path = arguments!["path"] as! String
MyCookieManager.deleteCookie(url: url, name: name, domain: domain, path: path, result: result);
break;
case "deleteCookies":
let url = arguments!["url"] as! String
let domain = arguments!["domain"] as! String
let path = arguments!["path"] as! String
MyCookieManager.deleteCookies(url: url, domain: domain, path: path, result: result);
break;
case "deleteAllCookies":
MyCookieManager.deleteAllCookies(result: result);
break;
default:
result(FlutterMethodNotImplemented)
break
}
}
public static func setCookie(url: String,
name: String,
value: String,
domain: String,
path: String,
expiresDate: Int?,
maxAge: Int?,
isSecure: Bool?,
result: @escaping FlutterResult) {
var properties: [HTTPCookiePropertyKey: Any] = [:]
properties[.originURL] = url
properties[.name] = name
properties[.value] = value
properties[.domain] = domain
properties[.path] = path
if expiresDate != nil {
properties[.expires] = NSDate(timeIntervalSince1970: Double(expiresDate!))
}
if maxAge != nil {
properties[.maximumAge] = String(maxAge!)
}
properties[.secure] = (isSecure != nil && isSecure!) ? "TRUE" : "FALSE"
let cookie = HTTPCookie(properties: properties)!
MyCookieManager.httpCookieStore!.setCookie(cookie)
result(true)
}
public static func getCookies(url: String, result: @escaping FlutterResult) {
var cookieList: [[String: Any]] = []
MyCookieManager.httpCookieStore!.getAllCookies { (cookies) in
for cookie in cookies {
if cookie.domain.contains(URL(string: url)!.host!) {
cookieList.append([
"name": cookie.name,
"value": cookie.value
])
}
}
result(cookieList)
}
}
public static func deleteCookie(url: String, name: String, domain: String, path: String, result: @escaping FlutterResult) {
MyCookieManager.httpCookieStore!.getAllCookies { (cookies) in
for cookie in cookies {
var originURL = ""
if cookie.properties![.originURL] is String {
originURL = cookie.properties![.originURL] as! String
}
else if cookie.properties![.originURL] is URL {
originURL = (cookie.properties![.originURL] as! URL).absoluteString
}
if (!originURL.isEmpty && originURL != url) {
continue
}
if cookie.domain.contains(domain) && cookie.name == name && cookie.path == path {
MyCookieManager.httpCookieStore!.delete(cookie, completionHandler: {
result(true)
})
return
}
}
result(false)
}
}
public static func deleteCookies(url: String, domain: String, path: String, result: @escaping FlutterResult) {
MyCookieManager.httpCookieStore!.getAllCookies { (cookies) in
for cookie in cookies {
var originURL = ""
if cookie.properties![.originURL] is String {
originURL = cookie.properties![.originURL] as! String
}
else if cookie.properties![.originURL] is URL{
originURL = (cookie.properties![.originURL] as! URL).absoluteString
}
if (!originURL.isEmpty && originURL != url) {
continue
}
if cookie.domain.contains(domain) && cookie.path == path {
MyCookieManager.httpCookieStore!.delete(cookie, completionHandler: nil)
}
}
result(true)
}
}
public static func deleteAllCookies(result: @escaping FlutterResult) {
let websiteDataTypes = NSSet(array: [WKWebsiteDataTypeCookies])
let date = NSDate(timeIntervalSince1970: 0)
WKWebsiteDataStore.default().removeData(ofTypes: websiteDataTypes as! Set<String>, modifiedSince: date as Date, completionHandler:{
result(true)
})
}
}

View File

@ -55,6 +55,12 @@ public class SwiftFlutterPlugin: NSObject, FlutterPlugin {
let channel = FlutterMethodChannel(name: "com.pichillilorenzo/flutter_inappbrowser", binaryMessenger: registrar.messenger())
let instance = SwiftFlutterPlugin(with: registrar)
registrar.addMethodCallDelegate(instance, channel: channel)
if #available(iOS 11.0, *) {
MyCookieManager(registrar: registrar)
} else {
// Fallback on earlier versions
}
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {

View File

@ -1133,14 +1133,21 @@ class CookieManager {
}
///Sets a cookie for the given [url]. Any existing cookie with the same [host], [path] and [name] will be replaced with the new cookie. The cookie being set will be ignored if it is expired.
static Future<void> setCookie(String url, String name, String value, String domain,
{ String path = "/",
///
///The default value of [path] is `"/"`.
///If [domain] is `null`, its default value will be the domain name of [url].
static Future<void> setCookie(String url, String name, String value,
{ String domain,
String path = "/",
int expiresDate,
bool isHTTPOnly,
int maxAge,
bool isSecure }) async {
if (!_initialized)
_init();
if (domain == null)
domain = _getDomainName(url);
assert(url != null && url.isNotEmpty);
assert(name != null && name.isNotEmpty);
assert(value != null && value.isNotEmpty);
@ -1153,8 +1160,8 @@ class CookieManager {
args.putIfAbsent('value', () => value);
args.putIfAbsent('domain', () => domain);
args.putIfAbsent('path', () => path);
args.putIfAbsent('expiresDate', () => expiresDate);
args.putIfAbsent('isHTTPOnly', () => isHTTPOnly);
args.putIfAbsent('expiresDate', () => expiresDate.toString());
args.putIfAbsent('maxAge', () => maxAge);
args.putIfAbsent('isSecure', () => isSecure);
await _channel.invokeMethod('setCookie', args);
@ -1175,10 +1182,10 @@ class CookieManager {
return cookies;
}
///Gets a cookie by its [cookieName] for the given [url].
static Future<Map<String, dynamic>> getCookie(String url, String cookieName) async {
///Gets a cookie by its [name] for the given [url].
static Future<Map<String, dynamic>> getCookie(String url, String name) async {
assert(url != null && url.isNotEmpty);
assert(cookieName != null && cookieName.isNotEmpty);
assert(name != null && name.isNotEmpty);
Map<String, dynamic> args = <String, dynamic>{};
args.putIfAbsent('url', () => url);
@ -1186,9 +1193,63 @@ class CookieManager {
cookies = cookies.cast<Map<dynamic, dynamic>>();
for(var i = 0; i < cookies.length; i++) {
cookies[i] = cookies[i].cast<String, dynamic>();
if (cookies[i]["name"] == cookieName)
if (cookies[i]["name"] == name)
return cookies[i];
}
return null;
}
///Removes a cookie by its [name] for the given [url], [domain] and [path].
///The default value of [path] is `"/"`.
///If [domain] is `null` or empty, its default value will be the domain name of [url].
static Future<void> deleteCookie(String url, String name, {String domain = "", String path = "/"}) async {
if (domain == null || domain.isEmpty)
domain = _getDomainName(url);
assert(url != null && url.isNotEmpty);
assert(name != null && name.isNotEmpty);
assert(domain != null && url.isNotEmpty);
assert(path != null && url.isNotEmpty);
Map<String, dynamic> args = <String, dynamic>{};
args.putIfAbsent('url', () => url);
args.putIfAbsent('name', () => name);
args.putIfAbsent('domain', () => domain);
args.putIfAbsent('path', () => path);
await _channel.invokeMethod('deleteCookie', args);
}
///Removes all cookies for the given [url], [domain] and [path].
///The default value of [path] is `"/"`.
///If [domain] is `null` or empty, its default value will be the domain name of [url].
static Future<void> deleteCookies(String url, {String domain = "", String path = "/"}) async {
if (domain == null || domain.isEmpty)
domain = _getDomainName(url);
assert(url != null && url.isNotEmpty);
assert(domain != null && url.isNotEmpty);
assert(path != null && url.isNotEmpty);
Map<String, dynamic> args = <String, dynamic>{};
args.putIfAbsent('url', () => url);
args.putIfAbsent('domain', () => domain);
args.putIfAbsent('path', () => path);
await _channel.invokeMethod('deleteCookies', args);
}
///Removes all cookies.
static Future<void> deleteAllCookies() async {
Map<String, dynamic> args = <String, dynamic>{};
await _channel.invokeMethod('deleteAllCookies', args);
}
static String _getDomainName(String url) {
Uri uri = Uri.parse(url);
String domain = uri.host;
if (domain == null)
return "";
return domain.startsWith("www.") ? domain.substring(4) : domain;
}
}