Last active
December 14, 2015 17:09
-
-
Save quique123/5120205 to your computer and use it in GitHub Desktop.
first run check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(void)checkIfFirstRun{ | |
NSString *bundleVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey]; | |
NSString *appFirstStartOfVersionKey = [NSString stringWithFormat:@"first_start_%@", bundleVersion]; | |
NSNumber *alreadyStartedOnVersion = [[NSUserDefaults standardUserDefaults] objectForKey:appFirstStartOfVersionKey]; | |
if(!alreadyStartedOnVersion || [alreadyStartedOnVersion boolValue] == NO) { | |
// Preload plist data | |
[self parsePlistIntoCD]; | |
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] forKey:appFirstStartOfVersionKey]; | |
} | |
} | |
-(void)parsePlistIntoCD{ | |
NSString *fullFileName = @"PreloadedFarsiman.plist"; | |
NSString *plistPath; | |
// 1: Get the Path to the plist file | |
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0]; | |
plistPath = [rootPath stringByAppendingPathComponent:fullFileName]; | |
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) { | |
plistPath = [[NSBundle mainBundle] pathForResource:@"PreloadedFarsiman" ofType:@"plist"]; | |
} | |
// 2: Read in the plist file | |
NSDictionary *plistDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath]; | |
//Now put the plistDictionary into CD... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (NSURL *)applicationCacheDirectory{ | |
return [[[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject]; | |
} | |
- (NSURL *)JSONDataRecordsDirectory{ | |
NSFileManager *fileManager = [NSFileManager defaultManager]; | |
NSURL *url = [NSURL URLWithString:@"JSONRecords/" relativeToURL:[self applicationCacheDirectory]]; | |
NSError *error = nil; | |
if (![fileManager fileExistsAtPath:[url path]]) { | |
[fileManager createDirectoryAtPath:[url path] withIntermediateDirectories:YES attributes:nil error:&error]; | |
} | |
return url; | |
} | |
- (void)writeJSONResponse:(id)response toDiskForClassWithName:(NSString *)className { | |
NSURL *fileURL = [NSURL URLWithString:className relativeToURL:[self JSONDataRecordsDirectory]]; | |
if (![(NSDictionary *)response writeToFile:[fileURL path] atomically:YES]) { | |
NSLog(@"Error saving response to disk, will attempt to remove NSNull values and try again."); | |
// remove NSNulls and try again... | |
NSArray *records = [response objectForKey:@"results"]; | |
NSMutableArray *nullFreeRecords = [NSMutableArray array]; | |
for (NSDictionary *record in records) { | |
NSMutableDictionary *nullFreeRecord = [NSMutableDictionary dictionaryWithDictionary:record]; | |
[record enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { | |
if ([obj isKindOfClass:[NSNull class]]) { | |
[nullFreeRecord setValue:nil forKey:key]; | |
} | |
}]; | |
[nullFreeRecords addObject:nullFreeRecord]; | |
} | |
NSDictionary *nullFreeDictionary = [NSDictionary dictionaryWithObject:nullFreeRecords forKey:@"results"]; | |
if (![nullFreeDictionary writeToFile:[fileURL path] atomically:YES]) { | |
NSLog(@"Failed all attempts to save reponse to disk: %@", response); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment