Skip to content

Instantly share code, notes, and snippets.

@ericleiyang
Created July 27, 2020 11:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericleiyang/c4c13e3b9cc572988d17a65a2d9fb811 to your computer and use it in GitHub Desktop.
Save ericleiyang/c4c13e3b9cc572988d17a65a2d9fb811 to your computer and use it in GitHub Desktop.
import UIKit
import WebKit
import XCPlayground
func testWebCookie() {
var webView = WKWebView()
// Expiry in 30 days
let days: TimeInterval = 30 * 24 * 60 * 60
guard let cookie = HTTPCookie(properties: [
.domain: "domain",
.path: "/",
.name: "ath_access_token",
.value: "accessToken",
.secure: "true",
.expires: NSDate(timeIntervalSinceNow: days)
]) else { return }
// WKWebsiteDataStore
let websiteDataStore = WKWebsiteDataStore.nonPersistent()
// WKWebViewConfiguration
let configuration = WKWebViewConfiguration()
configuration.websiteDataStore = websiteDataStore
// WKWebView with non persistent data store
let webView = WKWebView(frame: CGRect.zero, configuration: configuration)
webView.configuration.websiteDataStore.httpCookieStore.setCookie(cookie) {
webView.configuration.websiteDataStore.httpCookieStore.getAllCookies { cookies in
print("cookis: \(cookies)")
/**
cookis: [<NSHTTPCookie
version:1
name:ath_access_token
value:accessToken
expiresDate:'2020-08-26 09:57:09 +0000'
created:'2020-07-27 09:57:09 +0000'
sessionOnly:FALSE
domain:domain
partition:none
sameSite:none
path:/
isSecure:TRUE
path:"/" isSecure:TRUE>]
*/
}
}
HTTPCookieStorage.shared.setCookie(cookie)
XCPlaygroundPage.currentPage.liveView = webView
}
testWebCookie()
@shravanteegala
Copy link

i'm getting below errors:

2023-11-15 20:23:40.384873+0530 TestIOSCookies[3319:115883] [Process] NetworkProcessProxy::getNetworkProcessConnection: connection identifier is empty
cookis: []
2023-11-15 20:23:40.386718+0530 TestIOSCookies[3319:115883] [Process] getNetworkProcessConnection: Failed to get connection to network process, will reply invalid identifier ...
2023-11-15 20:24:10.384040+0530 TestIOSCookies[3319:115883] Could not signal service com.apple.WebKit.WebContent: 113: Could not find specified service

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment