I have written a class called AppHelper which has class level (static) methods. Those methods are very useful to use within UIViewController.
AppHelper.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// // AppHelper.h // #import <foundation /Foundation.h> @interface AppHelper : NSObject { } +(NSString *)getDocsDirectory; +(BOOL)setPlist:(NSString *)strPlistName; +(void)setPlistData:(NSString *)strPlistName key:(NSString *)key strValue:(NSString *)strValue; +(id)getPlistData:(NSString *)strPlistName key:(NSString *)key; @end |
AppHelper.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 |
// // AppHelper.m // #import "AppHelper.h" @implementation AppHelper +(NSString *)getDocsDirectory { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; return documentsDirectory; } +(BOOL)setPlist:(NSString *)strPlistName { NSError *error; NSString *path = [[self getDocsDirectory] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", strPlistName]]; NSFileManager *fileManager = [NSFileManager defaultManager]; if (![fileManager fileExistsAtPath:path]) { NSString *bundle = [[NSBundle mainBundle] pathForResource:strPlistName ofType:@"plist"]; [fileManager copyItemAtPath:bundle toPath:path error:&error]; return YES; } else { return NO; } } +(void)setPlistData:(NSString *)strPlistName key:(NSString *)key strValue:(NSString *)strValue { [self setPlist:strPlistName]; NSString *path = [[self getDocsDirectory] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", strPlistName]]; NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:path]; [data setObject:strValue forKey:key]; //[data setObject:[NSNumber numberWithInt:strValue] forKey:key]; [data writeToFile:path atomically:YES]; } +(id)getPlistData:(NSString *)strPlistName key:(NSString *)key { [self setPlist:strPlistName]; NSString *path = [[self getDocsDirectory] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", strPlistName]]; NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path]; return [data objectForKey:key]; } |