3

I found an interesting fact that the shared NSHTTPCookieStorage will sync persistent cookies(isSessionOnly=false) to WKWebsiteDataStore.defaultDataStore.httpCookieStore, could anyone tell me if this is a feature or kind of unreliable behavior? No documentation ever found for this...

Tested on iOS 13.2.2 & 12.1

POC

Step1. Create a persistent cookie

var props = Dictionary<HTTPCookiePropertyKey, Any>()
props[HTTPCookiePropertyKey.name] = "HelloWorld"
props[HTTPCookiePropertyKey.value] = "666"
props[HTTPCookiePropertyKey.path] = "/"
props[HTTPCookiePropertyKey.domain] = ".wtf.com"
props[HTTPCookiePropertyKey.expires] = Date(timeIntervalSinceNow: 60 * 60)

let cookie = HTTPCookie(properties: props)
HTTPCookieStorage.shared.setCookie(cookie!)

Step2. Read the previous cookie from the default WK storage

WKWebsiteDataStore.default().httpCookieStore.getAllCookies { (cookies) in
    for cookie in cookies {
        if cookie.name == "HelloWorld" { 
            NSLog("boommmmmmm!")
            return
        }
    }
}
Cyme
  • 31
  • 3

0 Answers0