How can I save NSData to a persistent local file in iOS SDK?
The answer is pretty simple: use this helpful Objective-C method.
You need:
- http://objective-c-functions.blogspot.com/2013/07/create-directory-in-ios-file-system.html
/** Saves data to device filesystem and returns its path.
@param data Data to save.
@param fileName String representing the name of the file in which data get stored.
@param localFolder Full path of folder that contains the file.
@return Returns file path of saved data.
*/
+ (NSString *)saveData:(NSData *)data toLocalFile:(NSString *)fileName inFolder:(NSString *)localFolder {
// If folder does not exist then it will be created on the fly.
BOOL isDir;
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:localFolder isDirectory:&isDir];
if (!exists || !isDir) {
[LMFunctions createFolderWithName:localFolder];
}
// Save the file.
NSString *filePath = [localFolder stringByAppendingPathComponent:fileName];
[data writeToFile:filePath atomically:YES];
return filePath;
}
Download the ready-for-use source file (.m) of Save raw data to a local file
No comments:
Post a Comment