Skip to content

Instantly share code, notes, and snippets.

@maciekish
Last active February 25, 2021 22:04
Show Gist options
  • Save maciekish/6268142 to your computer and use it in GitHub Desktop.
Save maciekish/6268142 to your computer and use it in GitHub Desktop.
Persists UIWebView cookies to disk. To send the cookies with an initial NSURLRequest you must do the following after loading the cookies: NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:yourURL]; NSDictionary* headers = [NSHTTPCookie requestHeaderFieldsWithCookies:[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]]; [requ…
//
// NSHTTPCookieStorage+FreezeDry.h
//
// Created by Maciej Swic on 19/08/13.
//  
// 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;
@end
//
// NSHTTPCookieStorage+FreezeDry.m
//
// Created by Maciej Swic on 19/08/13.
//  
// 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:@"%d", cookie.version];
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]];
}
}
- (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