-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathAuthorization.m
83 lines (65 loc) · 2.28 KB
/
Authorization.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//
// Authorization.m
//
// Created by Maxime Guilbot on 12/8/10.
// Copyright 2010 ekohe. All rights reserved.
//
#import "Authorization.h"
@interface Authorization (PrivateMethods)
+(void) installPlistTemplateIfNeeded;
+(NSString*) pathToPlistFile;
+(NSDictionary*) readPlistFile;
+(id) readValueInPlistFileForKey:(NSString*)key;
+(void) writeToPlistFileKey:(NSString*)key value:(id)value;
@end
@implementation Authorization
+(void) deleteAuthorization {
// Delete cookies for the web view
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies]) {
[storage deleteCookie:cookie];
}
[self writeToPlistFileKey:@"cookies" value:[NSArray array]];
}
#pragma mark -
#pragma mark Cookies Management
+(void) setCookies:(NSArray*)cookies {
[self writeToPlistFileKey:@"cookies" value:cookies];
}
+(NSArray*) cookies {
return (NSArray*)[self readValueInPlistFileForKey:@"cookies"];
}
#pragma mark -
#pragma mark Private API - read and write of the values in the plist file
+(id) readValueInPlistFileForKey:(NSString*)key {
return [[self readPlistFile] valueForKey:key];
}
+(void) writeToPlistFileKey:(NSString*)key value:(id)value {
NSDictionary *plist = [[self readPlistFile] retain];
[plist setValue:value forKey:key];
[plist writeToFile:[self pathToPlistFile] atomically:NO];
[plist release];
#ifdef DEBUG_PLIST_WRITES
NSLog(@"[PList] Saving %@ to %@", key, value);
#endif
}
#pragma mark -
#pragma mark Private API - plist file management
+(void) installPlistTemplateIfNeeded {
if (![[NSFileManager defaultManager] fileExistsAtPath:[self pathToPlistFile]]) {
NSString *plistTemplatePath = [[NSBundle mainBundle] pathForResource:@"Authorization" ofType:@"plist"];
if (plistTemplatePath) {
[[NSFileManager defaultManager] copyItemAtPath:plistTemplatePath toPath:[self pathToPlistFile] error:NULL];
}
}
}
+ (NSString *)pathToPlistFile {
return [[[Utilities appDelegate] applicationDocumentsDirectory] stringByAppendingPathComponent:@"Authorization.plist"];
}
+(NSDictionary*) readPlistFile {
[self installPlistTemplateIfNeeded];
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:[self pathToPlistFile]];
return [plistDict autorelease];
}
@end