IOS cookies will be destroyed upon completion. How to prevent this or save this cookie?

I am sending HTTP session cookie requests stored on the device side. The problem is that I want to save it at the end of the application. But it looks like all the cookies in my application are being deleted. I tried both the simulator and the device and they got the same behavior.

Is there an iOS way to prevent deleting this file? If not, how can I save it to disk and restore it?

I plan to keep this cookie in the iOS keychain for security. And I think that 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 back to use getting the original cookie. The problem is that I don’t want to iterate over the problems with the conversion of NSDictionary → String and parse String → NSDictionary .

+3
source share
3 answers

===== SUMMARY =====

Well, I'm going to create another answer to summarize the possible ways to save an NSDictionary (which does not contain objects, of course!).

- NSDictionary writeToFile:. plist. ( NSArray) , NSData .

, , - NSDictionary , JSON XML. iOS . (SFHFKeychainUtils ). keychain - .

+1

. JSONKit ( RestKit) NSString NSDictionary JSON. SFHFKeychainUtils, . , cookie .:)

#import "RestKit/JSONKit.h"

#define WSC_serviceName @"WSC_serviceName"
#define WSC_username    @"WSC_username"
#define WSC_cookieName  @"_your_cookie_name" // name of the session cookie

- (void)saveSessionCookieToKeyChain {
    for (NSHTTPCookie *c in [NSHTTPCookieStorage sharedHTTPCookieStorage].cookies) {
        if ([c.name isEqualToString:WSC_cookieName]) {
            NSString *cookieString = [c.properties JSONString];
            NSLog(@"cookieString: %@",cookieString);
            NSError *saveError = nil;
            [SFHFKeychainUtils storeUsername:WSC_username
                                 andPassword:cookieString 
                              forServiceName:WSC_serviceName 
                              updateExisting:YES error:&saveError];
        }
    }
}

- (BOOL)readsSessionCookieFromKeyChain {
    NSError *readError = nil;
    NSString *jsonString = [SFHFKeychainUtils getPasswordForUsername:WSC_username
                                                      andServiceName:WSC_serviceName 
                                                               error:&readError];
    if (!jsonString) return NO;
    NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; // be sure that data is in UTF8 or it won't work
    JSONDecoder* decoder = [[JSONDecoder alloc] initWithParseOptions:JKParseOptionNone];
    NSDictionary* jsonDict = (NSDictionary*)[decoder objectWithData:jsonData];
    NSLog(@"jsonDict: %@",jsonDict);

    NSHTTPCookie *cookie = [[NSHTTPCookie alloc] initWithProperties:jsonDict];
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];

    return cookie!=nil;
}

- (void)clearSessionCookieAndRemoveFromKeyChain {
    for (NSHTTPCookie *c in [NSHTTPCookieStorage sharedHTTPCookieStorage].cookies) {
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:c];
    }
    NSError *deleteError = nil;
    [SFHFKeychainUtils deleteItemForUsername:WSC_username 
                              andServiceName:WSC_serviceName 
                                       error:&deleteError];
}
+2

, NSHTTPURLResponse, cookie:

NSDictionary * headers = [(NSHTTPURLResponse *)response allHeaderFields];
NSArray * cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:headers forURL:response.URL];

response - NSHTTPURLResponse.

NSURLResponses NSURLConnectionDelegate

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse;
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;

NSHTTPCookie has a property propertiesthat returns NSDictionary, so you can save them. Then they can be created with the same dictionary using-initWithProperties:

To send them, you need to create your own cookie header line NSURLRequest. Sort of:

NSMutableString * cookieString = [[NSMutableString alloc] init];
for (NSHTTPCookie * cookie in myLoadedCookies){
    [cookieString appendFormat:@"%@=%@; ", cookie.name, cookie.value];
}
[request setValue:cookieString forHTTPHeaderField:@"Cookie"];
[cookieString release];

Where request- NSMutableURLRequest.

You must also be sure that the iOS control core itself must be locked:

[request setHTTPShouldHandleCookies:NO];
+1
source

All Articles