`
cppmule
  • 浏览: 447146 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

iPhone: NSHTTPCookie is not saved across app restarts

    博客分类:
  • iOS
 
阅读更多

iPhone: NSHTTPCookie is not saved across app restarts

 

 

In my iPhone app, I want to be able to reuse the same server-side session when my app restarts. A session on the server is identified by a cookie, which is sent on each request. When I restart the app, that cookie is gone and I can't use the same session anymore.

 

What I noticed when I used the NSHTTPCookieStorage to look up the cookie I got from the server, is that [cookie isSessionOnly] returns YES. I get the impression that this is why cookies are not saved across restarts of my app. What would I have to do to make my cookie NOT session only? What HTTP headers do I have to send from the server?

 

 

accepted

You can save the cookie by saving its properties dictionary and then restoring as a new cookiebefore you go to re-connect.

 

Save:

 

NSArray* allCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:URL]];

for (NSHTTPCookie *cookie in allCookies) {

    if ([cookie.name isEqualToString:MY_COOKIE]) { 

        NSMutableDictionary* cookieDictionary = [NSMutableDictionary dictionaryWithDictionary:[[NSUserDefaults standardUserDefaults] dictionaryForKey:PREF_KEY]];

        [cookieDictionary setValue:cookie.properties forKey:URL];

        [[NSUserDefaults standardUserDefaults] setObject:cookieDictionary forKey:PREF_KEY];

    }

 }

Load:

 

NSDictionary* cookieDictionary = [[NSUserDefaults standardUserDefaults] dictionaryForKey:PREF_KEY];

NSDictionary* cookieProperties = [cookieDictionary valueForKey:URL];

if (cookieProperties != nil) {

    NSHTTPCookie* cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];

    NSArray* cookieArray = [NSArray arrayWithObject:cookie];

    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookieArray forURL:[NSURL URLWithString:URL] mainDocumentURL:nil];

}

 

 

do you need to synchronize the nsuserdefaults? – Ninja Jan 10 at 8:52

You only need to synchronize if you need to save them right then. Otherwise they will be saved at some interderminate time later. Here's the doc page: developer.apple.com/library/mac/#documentation/Cocoa/Reference/… – Mike Katz Jan 10 at 18:44


分享到:
评论

相关推荐

    详解iOS App开发中Cookie的管理方法

    本文将深入探讨如何使用NSHTTPCookieStorage和NSHTTPCookie类来管理iOS App中的Cookie。 首先,理解Cookie的基本概念。Cookie是由服务器在用户的浏览器或设备上存储的一小段文本信息,通常用于标识用户身份和维护...

    UIWebView的cookie读取与保存

    它允许开发者在App内部展示HTML、CSS和JavaScript等Web技术构建的内容。而在涉及到用户登录状态或者个性化信息时,Cookie就成为了关键的数据存储机制。本文将详细讲解如何在UIWebView中进行Cookie的读取与保存。 ...

    详解iOS App开发中session和coockie的用户数据存储处理

    苹果提供了NSURLSession、NSHTTPCookieStorage和NSHTTPCookie类来帮助开发者有效地处理这些功能。 NSURLSession是iOS7及更高版本中推荐的网络请求框架,它取代了旧的NSURLConnection。NSURLSession提供了三种不同的...

    iOS UIWebView 通过 cookie 完成自动登录实例

    let isSecure = cookie.isSecure let properties = [ NSHTTPCookie.name: cookie.name, NSHTTPCookie.value: cookie.value, NSHTTPCookie.domain: cookie.domain, NSHTTPCookie.path: cookie.path, ...

    OC-WKWebView基础

    [cookieStore getAllCookies:^(NSArray<NSHTTPCookie *> * _Nonnull cookies) { NSLog(@"Cookies: %@", cookies); }]; ``` ### 结语 `WKWebView`是iOS开发中不可或缺的工具,它的强大功能使得我们可以无缝集成Web...

    ASIhttp得力助手

    4. **处理cookies**:ASIHTTP自动处理服务器返回的cookies,可以通过`- (void)addCookie:(NSHTTPCookie *)cookie`手动添加cookie,或者通过`- (NSArray *)cookies`获取当前请求的所有cookies。 5. **请求队列管理**...

    IOS 网络请求中设置cookie

    NSHTTPCookie *cookie = [[NSHTTPCookie alloc] initWithProperties:properties]; return [NSMutableArray arrayWithObject:cookie]; } [request setRequestCookies:[self retrunCookies]]; ``` 这段代码会...

    swift-两步实现iOSUIWebView通过cookie完成自动登录。

    let cookie = NSHTTPCookie(properties: [ .name: "username", .value: "testUser", .domain: "yourDomain.com", .path: "/", .secure: true, // 如果是HTTPS连接,设置为true .httpOnly: true // 防止...

    一篇文章搞定iOS的Cookie存取

    NSHTTPCookieName: "fromapp", NSHTTPCookieValue: "ios", NSHTTPCookieDomain: kDomain, NSHTTPCookieOriginURL: kDomain, NSHTTPCookiePath: "/", NSHTTPCookieVersion: "0" ] let cookie = NSHTTPCookie...

    asihttprequest使用指南

    NSHTTPCookie *cookie = ...; [request addCookie:cookie]; ``` #### 八、处理压缩的响应,以及压缩请求body ##### 7.1 处理压缩的响应 默认情况下,ASIHTTPRequest会自动处理gzip和deflate压缩的响应。 ```...

    iOS AFNetworking中cookie重定向代码

    ` 这一步通过`NSHTTPCookie`类的静态方法`requestHeaderFieldsWithCookies:`,从当前保存的cookies中创建出一个包含所有cookie的请求头字段字典。这些headerFields将在后续的网络请求中作为请求头发送出去,使得...

    iOS WKWebView适配实战篇

    if ([cookies.lastObject isEqual:cookie]) { [self wkwebviewSetCookieSuccess]; } }]; } } ``` 3. WKWebView初始化时同步cookie WKWebView适配实战篇中还讨论了WKWebView初始化时同步cookie的问题。...

    iOS中关于Cookie验证登录状态

    [NSHTTPCookie] NSHTTPCookieStorage.shared.setCookies(cookies ?? [], for: nil, mainDocumentURL: nil) } ``` 4. **注销登录与清理Cookie**: 当用户选择退出登录,应清除所有与用户身份相关的数据。这包括...

Global site tag (gtag.js) - Google Analytics