Skip to content

Instantly share code, notes, and snippets.

@NicosKaralis
Created July 11, 2012 12:17
Show Gist options
  • Save NicosKaralis/3090009 to your computer and use it in GitHub Desktop.
Save NicosKaralis/3090009 to your computer and use it in GitHub Desktop.
FileHelper
#import <Foundation/Foundation.h>
@interface FileHelper : NSObject
+(NSString *) getDataFilePath;
+(NSString *) getConfigFilePath;
+(NSString *) getPlistFilePathWithName:(NSString *)name;
+(BOOL) removeFileAtPath:(NSString *)path;
@end
#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment