I am trying to use cookies in iOS WKWebView like this:
import UIKit
import WebKit
class ViewWrapper: UIViewController, WKNavigationDelegate{
@IBOutlet weak var viewerWebKit: WKWebView!
var loginToken: String?
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "url")!
let newcookie = HTTPCookie(properties: [
.domain: "domain",
.path: "/",
.name: "cookie name",
.value: "cookie value",
.secure: "FALSE",
.expires: NSDate(timeIntervalSinceNow: 31556926)
])
var request = URLRequest(url: url)
request.httpShouldHandleCookies = true
viewerWebKit.configuration.websiteDataStore.httpCookieStore.setCookie(newcookie!, completionHandler: {
print("cookie setup done")
self.viewerWebKit.load(request)
})
let refresh = UIBarButtonItem(barButtonSystemItem: .refresh, target: webView, action: #selector(viewerWebKit.reload))
toolbarItems = [refresh]
navigationController?.isToolbarHidden = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func loadView() {
viewerWebKit = WKWebView()
viewerWebKit.navigationDelegate = self
view = viewerWebKit
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
title = webView.title
}
}
Domain, path, name and value are 100% correct. But when I am trying to get them and print then on my website, no cookies are set. Website printing of cookies works well, because I did an android app for this and it worked well there.
Do I need to do something more to accept or store the cookies?