1

I am trying to set cookies in my iOS like this:

    let url = URL(string: "url")!
    let jar = HTTPCookieStorage.shared
    let cookieHeaderField = ["Set-Cookie": "key1=value1, key2=value2"]
    let cookies = HTTPCookie.cookies(withResponseHeaderFields: cookieHeaderField, for: url)
    jar.setCookies(cookies, for: url, mainDocumentURL: url)

    let request = URLRequest(url: url)

    viewerWebKit.load(request)

Then I am printing them like this:

    viewerWebKit.configuration.websiteDataStore.httpCookieStore.getAllCookies( { (cookies) in
        cookies.forEach({ (cookie) in
            print(cookie.name)
        })
    })

All cookies are printed and they seem to be set normally. But when I use the Web inspector of my safari to see, if they are really set then nothing is there. No cookies are set. What is the problem? Do I need to accept them? Is safari blocking them? How can I set them normally, to be visible in web inspector?

I also tried this approach:

import UIKit
import WebKit

class ViewWrapper: UIViewController, WKNavigationDelegate {

var loginToken: String?
@IBOutlet weak var viewerWebKit: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    var urlRequest = URLRequest(url: URL(string: "url")!)
    urlRequest.httpShouldHandleCookies = true

    let newcookie = HTTPCookie(properties: [
        .domain: "domain",
        .path: "",
        .name: "key",
        .value: "value",
        .secure: "FALSE",
        .expires: NSDate(timeIntervalSinceNow: 31556926)
        ])

    viewerWebKit.configuration.websiteDataStore.httpCookieStore.setCookie(newcookie!, completionHandler: {
        self.viewerWebKit.load(urlRequest)
    })

    viewerWebKit.configuration.websiteDataStore.httpCookieStore.getAllCookies( { (cookies) in
        cookies.forEach({ (cookie) in
            print(cookie.name)
        })
    })

}

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
}

func cookiesDidChange(in cookieStore: WKHTTPCookieStore) {
    cookieStore.getAllCookies({ (cookies) in
        cookies.forEach({ (cookie) in
            print(cookie.name)
        })
    })
}

}

but it didn't work too.

This is what I see in safari debug console: debug console

Cookies are not set.

This is what I see in Xcode's console.

xcode console

So here it seems to be set. But it is not in reality. Printing code prints cookies. But they are not all visible in safari console. How is that possible? Cookies csrftoken and sessionid are set by website, not by my app. And they are visible in both printing and debug console.

Matej Košút
  • 590
  • 2
  • 10
  • 27

1 Answers1

0

Set cookie through this

urlRequest.httpShouldHandleCookies = true

also after request create set

self.configuration.websiteDataStore.httpCookieStore.setCookie("your_http_cookie", completionHandler: {
    // Do whatever you want. I suggest loading your webview after cookie is set.
})

Implement this observers WKHTTPCookieStoreObserver in your class.

WKWebsiteDataStore.default().httpCookieStore.add(self)
func cookiesDidChange(in cookieStore: WKHTTPCookieStore) {
    // Must implement otherwise wkwebview cookie not sync properly
    self.httpCookieStore.getAllCookies { (cookies) in
        cookies.forEach({ (cookie) in
           // print your cookie here
        })
    }
}
blastervla
  • 539
  • 6
  • 19
Muhammad Shauket
  • 2,643
  • 19
  • 40
  • I tried this approach and just added my entire code and output to my question. You can have a look. – Matej Košút Oct 23 '18 at 09:12
  • if in console it showing then it mean it is setting to cookie space of wkwebview. – Muhammad Shauket Oct 23 '18 at 09:14
  • csrf cookie you are setting and i can see in image it is there on safari – Muhammad Shauket Oct 23 '18 at 09:15
  • Yes that one is set by website and is visible in both safari's console and Xcode's terminal. But those cookies that I am setting in my app, are visible only Xcode's terminal and not in safari's console. So they seem to be somehow blocked or not set properly – Matej Košút Oct 23 '18 at 09:18