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

Persisting Cookies In An iOS Application?

    博客分类:
  • iOS
 
阅读更多

Persisting Cookies In An iOS Application?

 

 

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?`

 

accepted

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.

 

share|improve this answer

answered Jan 4 '11 at 20:01

 

Magnus

705521

1  

Is MySavedCookies already there? Or do I have to store that when my app is about to go inactive? – Alex Jan 4 '11 at 20:05

1  

You have to save it yourself - It's just where I save mine. – Magnus Jan 4 '11 at 22:08

feedback

 

up vote

4

down vote

You shouldn't need to persist the cookies yourself as suggested in the other answer. NSHTTPCookieStorage will persist the cookies for you but you need to ensure that the cookies have an expiry date set on the server-side.

 

Cookies without an expiry date are considered 'session only' and will get cleared when you restart the app. You can check the 'session only' situation via a BOOL property in NSHTTPCookie. This is standard cookie stuff and not something specific to iOS.

分享到:
评论

相关推荐

    Beginning iOS 5 Application Development.pdf

    本书《开始iOS 5应用程序开发》由John Wiley & Sons, Inc.出版,旨在为初学者提供一套全面且易于理解的学习指南,涵盖iOS 5应用程序开发的基础知识及进阶技巧。通过一系列章节的讲解与实践,读者可以掌握如何构建、...

    Professional Android 4 Application Development 源代码

    Persisting the Application Instance State Including Static Files as Resources Working with the File System Chapter 8: Databases and Content Providers Introducing Android Databases Introducing SQLite ...

    iOS Fuondation Framework Reference

    It is commonly used for saving and restoring application state or persisting data. **Tasks**: - **Archiving Objects**: Methods for archiving objects to a stream or file. - **Handling Errors**: ...

    Manning.Spring.in.Action.4th.Edition.2014.11.epub

    1.2.1. Working with an application context 1.2.2. A bean’s life 1.3. Surveying the Spring landscape 1.3.1. Spring modules 1.3.2. The Spring portfolio 1.4. What’s new in Spring 1.4.1. What was new in...

    Beginning iPad Application Development

    ⊲⊲ Part II Disp laying and Persisting Data? Chapter 7 Using the Table View. 185 Chapter 8 Application Preferences. 233 Chapter 9 File Handling. 253 Chapter 10 Database Storage Using SQLite3. 269 ⊲...

    Express in Action: Writing, building, and testing Node.js applications

    Evan Hahn is an active member of the Node and Express community and contributes to many open source JavaScript projects. Table of Contents PART 1 INTRO What is Express? The basics of Node.js ...

    iOS打的LLDB学习资料-英文版

    LLDB (LLVM Debugger) 是苹果开发的一种强大的软件调试器,用于iOS、macOS以及其他Apple平台的开发。这篇iOS打的LLDB学习资料是英文版,涵盖了从基础到进阶的LLDB使用技巧,旨在帮助开发者更好地理解和使用LLDB进行...

    Laravel开发-persisting-requests

    在Laravel框架中,"persisting-requests"是一个专门用于处理持久请求的扩展包,它为开发者提供了一种高效的方法来管理和存储HTTP请求的状态,以便在后续操作中重用或恢复这些请求。这个包的核心目标是减少重复的HTTP...

    Advanced Apple Debugging & Reverse Engineering v0.9.5

    Using DTrace to observe iOS frameworks (like UIKit) can give you an incredible insight into how the authors designed their code. 26. Intermediate DTrace This chapter will act as a grab-bag of more ...

    Test-Drive ASP.NET MVC

    II Building an Application 3 Getting Organized with MVC 3.1 Time Management with GetOrganized 3.2 Reading Data 3.3 Creating a To-Do 3.4 Deleting: Creating an Action Without a View 3.5 Updating: ...

    Learning.Node.js.for..NET.Developers.epub

    Build an interactive web application with MongoDB and Redis and create your own JavaScript modules that work both on the client side and server side Familiarize yourself with the new features of Node....

    Learning Kotlin by building Android Applications

    This book adopts a project-style approach, where we focus on teaching Android development by building three different Android Application, namely Building you Tic-Tac-Toe application, Developing your ...

    Sublime.Text.Build.3069._Win_32bit 破解版

    Fixed Ignored Words not persisting after exiting Improved some error messages when parsing .tmLanguage files Improved URL detection in Open URL context menu item (thanks iamntz and facelessuser) Fixed...

    基于Google.App.Engine(GAE)的Java和GWT应用开发.pdf

    Chapter 4: Persisting Data: The App Engine Datastore Chapter 5: JDO Object Relationships and Queries Chapter 6: Implementing MVP, an Event Bus, and Other GWT Pattern Chapter 7: Background Processing ...

    Core Data:Apple’s API for Persisting Data on Mac OS X

    对于在 macOS 和 iOS 应用中处理复杂的数据模型的应用开发者来说,Core Data 提供了一个强大的解决方案。 #### 核心概念 **1. 模型层对象管理** - **对象图**:Core Data 使用对象图来组织和管理数据。每个对象都...

    Spark_for_Python

    The book includes several case studies and examples that demonstrate the practical application of Spark in real-world scenarios. These examples cover various domains such as social media analysis, ...

    NHibernate 2.0.1 + Movies + Sample Source Part1

    It handles persisting plain .NET objects to and from an underlying relational database. Given an XML description of your entities and relationships, NHibernate automatically generates SQL for loading...

    Entity Framework in action

    根据提供的文件信息,我们可以从中提取与Entity Framework相关的知识点,这本书名为《Entity Framework in action》,是一本专注于Entity Framework的书籍。以下是对文件内容的分析,整理出的知识点: 1. **书籍...

Global site tag (gtag.js) - Google Analytics