Skip to content

Commit

Permalink
Merge pull request pichillilorenzo#2 from jj-lin/Support-samesite-iOS12
Browse files Browse the repository at this point in the history
Support samesite cookie policy at iOS12
  • Loading branch information
plateaukao authored and GitHub Enterprise committed May 4, 2020
2 parents 6debfe6 + 19961be commit 82ec9b6
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions ios/Classes/MyCookieManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ 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_inappwebview_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 {
Expand All @@ -42,7 +42,7 @@ class MyCookieManager: NSObject, FlutterPlugin {
let isSecure = arguments!["isSecure"] as? Bool
let isHttpOnly = arguments!["isHttpOnly"] as? Bool
let sameSite = arguments!["sameSiteString"] as? String

MyCookieManager.setCookie(url: url, name: name, value: value, domain: domain, path: path, expiresDate: expiresDate, maxAge: maxAge, isSecure: isSecure, isHttpOnly: isHttpOnly,
sameSite: sameSite, result: result)
break
Expand Down Expand Up @@ -71,7 +71,7 @@ class MyCookieManager: NSObject, FlutterPlugin {
break
}
}

public static func setCookie(url: String,
name: String,
value: String,
Expand Down Expand Up @@ -100,19 +100,21 @@ class MyCookieManager: NSObject, FlutterPlugin {
if isHttpOnly == true {
properties[.httpOnly] = true
}

if #available(iOS 13, *) {
if sameSite != nil {
properties[.sameSitePolicy] = sameSite!

if let sameSite = sameSite {
if #available(iOS 13, *) {
properties[.sameSitePolicy] = sameSite
} else if #available(iOS 12, *) {
properties[.sameSite] = sameSite
}
}

let cookie = HTTPCookie(properties: properties)!
MyCookieManager.httpCookieStore!.setCookie(cookie, completionHandler: {() in
result(true)
})
}

public static func getCookies(url: String, result: @escaping FlutterResult) {
var cookieList: [[String: Any]] = []
MyCookieManager.httpCookieStore!.getAllCookies { (cookies) in
Expand All @@ -127,7 +129,7 @@ class MyCookieManager: NSObject, FlutterPlugin {
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 {
Expand All @@ -151,7 +153,7 @@ class MyCookieManager: NSObject, FlutterPlugin {
result(false)
}
}

public static func deleteCookies(url: String, domain: String, path: String, result: @escaping FlutterResult) {
MyCookieManager.httpCookieStore!.getAllCookies { (cookies) in
for cookie in cookies {
Expand All @@ -172,7 +174,7 @@ class MyCookieManager: NSObject, FlutterPlugin {
result(true)
}
}

public static func deleteAllCookies(result: @escaping FlutterResult) {
let websiteDataTypes = NSSet(array: [WKWebsiteDataTypeCookies])
let date = NSDate(timeIntervalSince1970: 0)
Expand All @@ -184,4 +186,7 @@ class MyCookieManager: NSObject, FlutterPlugin {

extension HTTPCookiePropertyKey {
static let httpOnly = HTTPCookiePropertyKey("HttpOnly")

@available(iOS 12, *)
static let sameSite = HTTPCookiePropertyKey("SameSite")
}

0 comments on commit 82ec9b6

Please sign in to comment.