Created
June 26, 2018 02:41
-
-
Save AllanChen/663933dac8e37bf54b0243489074964f to your computer and use it in GitHub Desktop.
write data to documents folder
This file contains hidden or 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)writeDataToFile:(NSString *)fileName wirteData:(NSData*)data{ | |
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:fileName]; | |
BOOL fileExists = [self fileExists:fileName]; | |
if(fileExists){ | |
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath]; | |
[fileHandle seekToEndOfFile]; | |
[fileHandle writeData:data]; | |
[fileHandle closeFile]; | |
} | |
else{ | |
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:fileName]; | |
BOOL writeSuccess = [data writeToFile:filePath atomically:YES]; | |
} | |
} | |
+ (BOOL)fileExists:(NSString *)fileName{ | |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
NSString *path = [paths objectAtIndex:0]; | |
NSString *filePath = [path stringByAppendingPathComponent:fileName]; | |
NSFileManager *fileManager = [NSFileManager defaultManager]; | |
BOOL result = [fileManager fileExistsAtPath:filePath]; | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment