Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dezinezync/c2b92a6f005cd291c639 to your computer and use it in GitHub Desktop.
Save dezinezync/c2b92a6f005cd291c639 to your computer and use it in GitHub Desktop.
//
// NSHTTPCookieStorage+FreezeDry.h
//
// Created by Maciej Swic on 19/08/13.
// Modified by Dezinezync on 06/08/14.
//  
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//  
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//  
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import <Foundation/Foundation.h>
@interface NSHTTPCookieStorage (FreezeDry)
- (void)save;
- (void)load;
- (void)purge;
- (NSString*)storagePath;
@end
//
// NSHTTPCookieStorage+FreezeDry.m
//
// Created by Maciej Swic on 19/08/13.
// Modified by Dezinezync on 06/08/14.
//  
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//  
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//  
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "NSHTTPCookieStorage+FreezeDry.h"
@implementation NSHTTPCookieStorage (FreezeDry)
- (void)save
{
NSMutableArray* cookieData = [NSMutableArray new];
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie* cookie in [cookieStorage cookies]) {
NSMutableDictionary* cookieDictionary = [NSMutableDictionary new];
cookieDictionary[NSHTTPCookieName] = cookie.name;
cookieDictionary[NSHTTPCookieValue] = cookie.value;
cookieDictionary[NSHTTPCookieDomain] = cookie.domain;
cookieDictionary[NSHTTPCookiePath] = cookie.path;
cookieDictionary[NSHTTPCookieSecure] = (cookie.isSecure ? @"YES" : @"NO");
cookieDictionary[NSHTTPCookieVersion] = [NSString stringWithFormat:@"%ld", (long)cookie.version];
// Don't set Expires as we'll need these to hang around, unless explicitly evicted.
// This way, the cookies stay in the store only during the app session.
// We should load them during app launch.
// if (cookie.expiresDate) cookieDictionary[NSHTTPCookieExpires] = cookie.expiresDate;
[cookieData addObject:cookieDictionary];
}
[cookieData writeToFile:[self storagePath] atomically:TRUE];
}
- (void)load
{
NSMutableArray* cookies = [NSMutableArray arrayWithContentsOfFile:[self storagePath]];
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSDictionary* cookieData in cookies) {
[cookieStorage setCookie:[NSHTTPCookie cookieWithProperties:cookieData]];
}
}
- (void)purge
{
NSError *deleteError;
BOOL deleted = [[NSFileManager defaultManager] removeItemAtPath:[self storagePath] error:&deleteError];
if(deleteError) LogID(deleteError);
LogBOOL(deleted, @"Cookies file purged ");
}
- (NSString*)storagePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
return [NSString stringWithFormat:@"%@/cookies.data", paths[0]];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment