jueves, 8 de junio de 2017

iOS: WKWebKit not persisting cookies (login in not persisting when navigation out and into the web view)

I was having an issue that when loging in to a page through a WKWebKit the session didn't stick. Everytime I arrived at the page I was prompted to login.

We have to do the following.

We need to create a unique process pool to be shared among all WKWebKits and pass it along as a parameter to all of them (this wasn't necessary on UIWebView the way Apple handles the coookies changed):

var processPool: WKProcessPool?

somewhere where we create the WebViewController we pass said pool:

let webViewController = WebViewController()
webViewController.url = "some-url"
webViewController.processPool = processPool

then the web view controller that contains the WKWebKit we configure it like so:

class WebViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
 
    var url: URL?
    var webView: WKWebView!
    var processPool: WKProcessPool?
 
    override func loadView() {
     
        let webConfiguration = WKWebViewConfiguration()
     
        if let pool = processPool {
            webConfiguration.processPool = pool
        }
     
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        webView.navigationDelegate = self
             
        view = webView
    }
...

}

No hay comentarios:

Publicar un comentario