I’m sending http requests with session-cookie stored on the device side. The problem is that I want to persist it across app termination. But it seems like all the cookies of my app get deleted. I tried on both the simulator and device and they got the same behavior.
Is there any iOS way to prevent this cookie deletion? If not, how can I save it to disk and recover it back?
I’m planning to save this cookie in iOS keychain for security. And I think all NSHTTPCookie properties can be safely converted to NSString. So my current idea is to convert from NSHTTPCookie -> NSDictionary -> String -> Save in Keychain, and go backward to use get the original cookie. The problem is I don’t want to go through the hassles of converting NSDictionary -> String and parsing String -> NSDictionary.
===== SUMMARY =====
Well, I’m going to create another answer just to summarize possible ways to persist a NSDictionary (that doesn’t contain objects of course!).
The easiest way to do this is to use NSDictionary’s
writeToFile:method. This will generate a plist file. (Also available in NSArray) If you’re worried about security, encrypt the array to an NSData object and write that.Another way, not the fastest one though, is to use a tool to convert NSDictionary to a string format such as JSON or XML. You can save this string in iOS keychain and retrieve it later easily. (
SFHFKeychainUtilsis a great for helping with keychain stuff). A good thing about using keychain is it is automatically encrypted.