UIWebView:跨应用重启保存cookie状态

时间:2015-07-31 23:47:16

标签: ios swift cookies uiwebview

我使用Swift和UIWebView开发应用程序,打开webapp。 每次我们的用户打开应用程序时,他们都需要再次登录,因为该应用程序不存储cookie。我该如何解决?

我试过这个来存储cookie:

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {

    println(request.URL!.absoluteString)


    var cookiesData = NSKeyedArchiver.archivedDataWithRootObject(NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies!)

    NSUserDefaults.standardUserDefaults().setObject(cookiesData, forKey: "savedCookies")

    return true
}

再次检索cookie:

NSHTTPCookieStorage.sharedHTTPCookieStorage().cookieAcceptPolicy = NSHTTPCookieAcceptPolicy.Always
let cookies = NSKeyedUnarchiver.unarchiveObjectWithData(NSUserDefaults.standardUserDefaults().objectForKey("cookies"))

我不知道如何在数组中检索cookie并迭代它们。

3 个答案:

答案 0 :(得分:2)

试试这段代码:

保存cookie

    let cookieJar: NSHTTPCookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
    let data: NSData = NSKeyedArchiver.archivedDataWithRootObject(cookieJar.cookies)
    let ud: NSUserDefaults = NSUserDefaults.standardUserDefaults()
    ud.setObject(data, forKey: "cookie")

检索cookie

    let ud: NSUserDefaults = NSUserDefaults.standardUserDefaults()
    let data: NSData? = ud.objectForKey("cookie") as? NSData
    if let cookie = data {
        let datas: NSArray? = NSKeyedUnarchiver.unarchiveObjectWithData(cookie) as? NSArray
        if let cookies = datas {
            for c in cookies as! [NSHTTPCookie] {
                NSHTTPCookieStorage.sharedHTTPCookieStorage().setCookie(c)
            }
        }
    }

答案 1 :(得分:1)

这是Objective-C中的答案:

保存Cookie:

NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:cookieJar.cookies];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:data forKey:@"Cookies"];

加载Cookie:

NSData *data = [defaults objectForKey:@"Cookies"];
if (data) {
    NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    if (cookies) {
        for (NSHTTPCookie *cookie in cookies) {
            [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
        }
    }
}

答案 2 :(得分:0)

SWIFT 4 (并且没有强制解包)

func saveCookie() {
    let cookieJar: HTTPCookieStorage = HTTPCookieStorage.shared
    if let cookies = cookieJar.cookies {
        let data: Data = NSKeyedArchiver.archivedData(withRootObject: cookies)
        let ud: UserDefaults = UserDefaults.standard
        ud.set(data, forKey: "cookie")
    }
}

func loadCookie() {
    let ud: UserDefaults = UserDefaults.standard
    let data: Data? = ud.object(forKey: "cookie") as? Data
    if let cookie = data {
        let datas: NSArray? = NSKeyedUnarchiver.unarchiveObject(with: cookie) as? NSArray
        if let cookies = datas {
            for c in cookies {
                if let cookieObject = c as? HTTPCookie {
                    HTTPCookieStorage.shared.setCookie(cookieObject)
                }
            }
        }
    }
}

例如,您可以在saveCookie()方法中致电didFinishNavigation(如果您使用的是webViewDidFinishLoad而非UIWebView,则可以在WKWebView中)。

您可以在loadCookie()或初始化viewDidLoad之前致电webView