Where to persistently store cookies from WebView IOS?

97 Views Asked by At

I am developing an app that supports multi accounts in different social medias. On this example view controller I have Instagram domain and "accName" variable. When the view controller is hidden, meaning removed from navigation stack, the cookies of this logged account are be saved to UserDefaults by associated key. They are removed from web view at the same time in order to have different accounts depending on which one the user choses. But it is only an example, and we cannot store cookies like that, as we need them to be held even when the app is killed.

So I am asking where exactly should I store the cookies for each account, so that I can load them every time user opens the app and choses one of their accounts? I am not sure if I can use Realm or Core Data, as it may be not very safe. But I am quite a newbie, so I may be wrong. Thanks a lot in advance

import UIKit
import WebKit

class WebViewController: UIViewController, WKNavigationDelegate {
    private var webView: WKWebView!
    
    var accName: String?

    override func viewDidLoad() {
        super.viewDidLoad()

        setupWebView()
        loadCookies()
    }

    private func setupWebView() {
        let configuration = WKWebViewConfiguration()
        webView = WKWebView(frame: view.bounds, configuration: configuration)
        webView.navigationDelegate = self
        view.addSubview(webView)
        
        let url = URL(string: "https://instagram.com")!
        let request = URLRequest(url: url)
        webView.load(request)
    }

    private func loadCookies() {
        CookieManager.shared.loadCookies(name: accName!)
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        CookieManager.shared.saveCookies(name: accName!)
    }
}

class CookieManager: NSObject {
    static let shared = CookieManager()
    
    func saveCookies(name: String) {
        let webView = WKWebView()
        WKWebsiteDataStore.default().httpCookieStore.getAllCookies { cookies in
            let cookieData = try? NSKeyedArchiver.archivedData(withRootObject: cookies, requiringSecureCoding: false)
            UserDefaults.standard.set(cookieData, forKey: name)
        }
    }
    
    func loadCookies(name: String) {
        if let cookieData = UserDefaults.standard.data(forKey: name),
           let cookies = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(cookieData) as? [HTTPCookie] {
            for cookie in cookies {
                WKWebsiteDataStore.default().httpCookieStore.setCookie(cookie)
            }
        }
    }
    
    func deleteCookies(name: String) {
        WKWebsiteDataStore.default().httpCookieStore.getAllCookies { cookies in
            for cookie in cookies {
                WKWebsiteDataStore.default().httpCookieStore.delete(cookie)
            }
        }
        UserDefaults.standard.removeObject(forKey: name)
    }
}

0

There are 0 best solutions below