在iOS应用程序中持续存在Cookie

在iOS应用程序中持续存在Cookie

本文介绍了在iOS应用程序中持续存在Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将在iOS应用程序中使用 NSHTTPCookieStorage 来管理从网址检索的Cookie,我理解它会在应用程序运行时管理Cookie。但是,我想知道是否可能在应用程序关闭后保留cookie。然后,当应用程序再次打开时,再次读取这些cookie。 NSHTTPCookieStorage 是否在应用程序使用之间保留Cookie?或者只是在应用程序运行时?我需要使用 CoreData 来保存这些cookie吗?

解决方案

您的应用加载时需要重新设置Cookie。我使用这样的代码:

  NSData * cookiesdata = [[NSUserDefaults standardUserDefaults] objectForKey:@MySavedCookies]; 
if([cookiesdata length]){
NSArray * cookies = [NSKeyedUnarchiver unarchiveObjectWithData:cookiesdata];
NSHTTPCookie * cookie;

(Cookie中的Cookie){
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}
}

b $ b

I am going to use NSHTTPCookieStorage in an iOS App to manage cookies that are retrieved from a url, and I understand that it will manage cookies during your application's runtime. However, I was wondering if it's possible to persist cookies after the application has closed. And then read those cookies again when the app is opened again. Does NSHTTPCookieStorage persist cookies between app uses? Or just during the applications runtime? Do I need to use CoreData to persist these cookies?`

解决方案

You need to re-set the cookies when your app is loaded. I use code like this:

NSData *cookiesdata = [[NSUserDefaults standardUserDefaults] objectForKey:@"MySavedCookies"];
if([cookiesdata length]) {
    NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData:cookiesdata];
    NSHTTPCookie *cookie;

    for (cookie in cookies) {
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
    }
}

and it works just fine.

这篇关于在iOS应用程序中持续存在Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 11:50