From b6b1e0e290f098c1d26236f20f0a29d748024861 Mon Sep 17 00:00:00 2001 From: Lorenzo Pichilli Date: Mon, 11 May 2020 15:09:17 +0200 Subject: [PATCH] fixed CookieManager.setCookie expiresDate option (#338) --- CHANGELOG.md | 1 + .../flutter_inappwebview/MyCookieManager.java | 8 +++++++- example/.flutter-plugins-dependencies | 2 +- ios/Classes/MyCookieManager.swift | 15 ++++++++++----- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3715a54d..c1c443bf 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## 3.1.1 - Fixed `Confirmation dialog (onbeforeunload) displayed after popped from webview page` [#337](https://github.com/pichillilorenzo/flutter_inappwebview/issues/337) +- Fixed `CookieManager.setCookie` `expiresDate` option ## 3.1.0 diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/MyCookieManager.java b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/MyCookieManager.java index 122b58a6..db8f37dd 100755 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/MyCookieManager.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/MyCookieManager.java @@ -1,6 +1,7 @@ package com.pichillilorenzo.flutter_inappwebview; import android.os.Build; +import android.util.Log; import android.webkit.CookieManager; import android.webkit.CookieSyncManager; import android.webkit.ValueCallback; @@ -10,6 +11,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.TimeZone; @@ -43,6 +45,8 @@ public class MyCookieManager implements MethodChannel.MethodCallHandler { String path = (String) call.argument("path"); String expiresDateString = (String) call.argument("expiresDate"); Long expiresDate = (expiresDateString != null ? new Long(expiresDateString) : null); + Log.d(LOG_TAG, expiresDateString + ""); + Log.d(LOG_TAG, expiresDate + ""); Integer maxAge = (Integer) call.argument("maxAge"); Boolean isSecure = (Boolean) call.argument("isSecure"); MyCookieManager.setCookie(url, name, value, domain, path, expiresDate, maxAge, isSecure, result); @@ -99,6 +103,8 @@ public class MyCookieManager implements MethodChannel.MethodCallHandler { cookieValue += ";"; + Log.d(LOG_TAG, cookieValue + ""); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { cookieManager.setCookie(url, cookieValue, new ValueCallback() { @Override @@ -217,7 +223,7 @@ public class MyCookieManager implements MethodChannel.MethodCallHandler { } public static String getCookieExpirationDate(Long timestamp) { - final SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy hh:mm:ss z"); + final SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy hh:mm:ss z", Locale.US); sdf.setTimeZone(TimeZone.getTimeZone("GMT")); return sdf.format(new Date(timestamp)); } diff --git a/example/.flutter-plugins-dependencies b/example/.flutter-plugins-dependencies index 286876b8..4819058c 100755 --- a/example/.flutter-plugins-dependencies +++ b/example/.flutter-plugins-dependencies @@ -1 +1 @@ -{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"connectivity","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/connectivity-0.4.8+5/","dependencies":[]},{"name":"flutter_downloader","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_downloader-1.4.4/","dependencies":[]},{"name":"flutter_inappwebview","path":"/Users/lorenzopichilli/Desktop/flutter_inappwebview/","dependencies":[]},{"name":"path_provider","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider-1.6.7/","dependencies":[]},{"name":"permission_handler","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/permission_handler-3.3.0/","dependencies":[]}],"android":[{"name":"connectivity","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/connectivity-0.4.8+5/","dependencies":[]},{"name":"flutter_downloader","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_downloader-1.4.4/","dependencies":[]},{"name":"flutter_inappwebview","path":"/Users/lorenzopichilli/Desktop/flutter_inappwebview/","dependencies":[]},{"name":"path_provider","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider-1.6.7/","dependencies":[]},{"name":"permission_handler","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/permission_handler-3.3.0/","dependencies":[]}],"macos":[{"name":"connectivity_macos","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/connectivity_macos-0.1.0+3/","dependencies":[]},{"name":"path_provider_macos","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider_macos-0.0.4+2/","dependencies":[]}],"linux":[],"windows":[],"web":[]},"dependencyGraph":[{"name":"connectivity","dependencies":["connectivity_macos"]},{"name":"connectivity_macos","dependencies":[]},{"name":"flutter_downloader","dependencies":[]},{"name":"flutter_inappwebview","dependencies":[]},{"name":"path_provider","dependencies":["path_provider_macos"]},{"name":"path_provider_macos","dependencies":[]},{"name":"permission_handler","dependencies":[]}],"date_created":"2020-05-11 13:40:48.620696","version":"1.17.0"} \ No newline at end of file +{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"connectivity","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/connectivity-0.4.8+5/","dependencies":[]},{"name":"flutter_downloader","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_downloader-1.4.4/","dependencies":[]},{"name":"flutter_inappwebview","path":"/Users/lorenzopichilli/Desktop/flutter_inappwebview/","dependencies":[]},{"name":"path_provider","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider-1.6.7/","dependencies":[]},{"name":"permission_handler","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/permission_handler-3.3.0/","dependencies":[]}],"android":[{"name":"connectivity","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/connectivity-0.4.8+5/","dependencies":[]},{"name":"flutter_downloader","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_downloader-1.4.4/","dependencies":[]},{"name":"flutter_inappwebview","path":"/Users/lorenzopichilli/Desktop/flutter_inappwebview/","dependencies":[]},{"name":"path_provider","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider-1.6.7/","dependencies":[]},{"name":"permission_handler","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/permission_handler-3.3.0/","dependencies":[]}],"macos":[{"name":"connectivity_macos","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/connectivity_macos-0.1.0+3/","dependencies":[]},{"name":"path_provider_macos","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider_macos-0.0.4+2/","dependencies":[]}],"linux":[],"windows":[],"web":[]},"dependencyGraph":[{"name":"connectivity","dependencies":["connectivity_macos"]},{"name":"connectivity_macos","dependencies":[]},{"name":"flutter_downloader","dependencies":[]},{"name":"flutter_inappwebview","dependencies":[]},{"name":"path_provider","dependencies":["path_provider_macos"]},{"name":"path_provider_macos","dependencies":[]},{"name":"permission_handler","dependencies":[]}],"date_created":"2020-05-11 15:01:02.801973","version":"1.17.0"} \ No newline at end of file diff --git a/ios/Classes/MyCookieManager.swift b/ios/Classes/MyCookieManager.swift index 289342a7..6fbb2651 100755 --- a/ios/Classes/MyCookieManager.swift +++ b/ios/Classes/MyCookieManager.swift @@ -37,8 +37,13 @@ class MyCookieManager: NSObject, FlutterPlugin { 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 + + var expiresDate: Int64? + if let expiresDateString = arguments!["expiresDate"] as? String { + expiresDate = Int64(expiresDateString) + } + + let maxAge = arguments!["maxAge"] as? Int64 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) @@ -74,8 +79,8 @@ class MyCookieManager: NSObject, FlutterPlugin { value: String, domain: String, path: String, - expiresDate: Int?, - maxAge: Int?, + expiresDate: Int64?, + maxAge: Int64?, isSecure: Bool?, result: @escaping FlutterResult) { var properties: [HTTPCookiePropertyKey: Any] = [:] @@ -85,7 +90,7 @@ class MyCookieManager: NSObject, FlutterPlugin { properties[.domain] = domain properties[.path] = path if expiresDate != nil { - properties[.expires] = NSDate(timeIntervalSince1970: Double(expiresDate!)) + properties[.expires] = Date(timeIntervalSince1970: TimeInterval(Double(expiresDate!)/1000)) } if maxAge != nil { properties[.maximumAge] = String(maxAge!)