public
Last active

FileHelper

  • Download Gist
FileHelper.h
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12
#import <Foundation/Foundation.h>
 
@interface FileHelper : NSObject
 
+(NSString *) getDataFilePath;
+(NSString *) getConfigFilePath;
 
+(NSString *) getPlistFilePathWithName:(NSString *)name;
 
+(BOOL) removeFileAtPath:(NSString *)path;
 
@end
FileHelper.m
Objective-C
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
#import "FileHelper.h"
 
#define DataFile @"data"
#define ConfigFile @"config"
 
@implementation FileHelper
 
+(NSString *) getDataFilePath {
return [self getPlistFilePathWithName:DataFile];
}
 
+(NSString *) getConfigFilePath {
return [self getPlistFilePathWithName:ConfigFile];
}
 
+(NSString *) getPlistFilePathWithName:(NSString *)name {
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[name stringByAppendingPathExtension:@"plist"]];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path]) {
NSString *bundle = [[NSBundle mainBundle] pathForResource:name ofType:@"plist"];
[fileManager copyItemAtPath:bundle toPath: path error:&error];
}
return path;
}
 
+(BOOL) removeFileAtPath:(NSString *)path {
NSError *error;
NSFileManager *filemgr = [NSFileManager defaultManager];
return [filemgr removeItemAtPath:path error:&error];
}
 
@end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.