Ever needed to save a singular URL response, for testing purposes and didn't want to use a persistancy framework like CoreData or Realm; Or a rarely subject-to-change static response that you probably saved in NSUserDefaults; Or just wanted to save a downloaded file for later retrieval.
Enter DADataManager to the rescue.
CocoaPods (recommended)
- Add
pod 'DADataManager'
to your podfile. - Add
#import <DADataManager/DADataManager.h>
(or@import DADataManager
if you build as a framework/Swift) in the class you want to use it.
- Clone the repository somwhere.
- Drop the folder DADataManager into your project.
- Add
#import "DADataManager.h"
in the class you want to use it.
DADataManager *manager = [DADataManager sharedManager];
let manager = DADataManager.sharedManager()
[manager imagesPathForFileName:@"image.jpg"]
(image stored in ../Documents/Data/images)
[manager dataFilesPathForFileName:@"file.json"]
(file stored in ../Documents/Data/files)
[manager rootPathForFileName:@"MyApp/Media/Videos/01.mp4"]
(file stored in ../MyApp/Media/Videos/01.mp4)
manager.libraryPathForFileName:("Samaritan/Core/Neural Nets/6741.SMRTN")
(file stored at path ../Library/Samaritan/Core/Neural Nets/6741.SMRTN)
manager.videosURLForFileName("1802.MP4")
(File URL for video at path ../Documents/Data/vidoes/1802.MP4)
NSArray *array = @[@"One", @"Two", @"Three"];
[manager saveObject:array toFilePath:[manager documentsPathForFileName:@"array.json"]];
let object = ["foo": 1, "bar": 2, "poo": 3, "jar": 4]
manager.saveObject(object, toFilePath: manager.dataFilesPathForFileName("file.dat"))
NSURLSession.sharedSession().dataTaskWithURL(URL, completionHandler: { (data, response, error) in
// Other stuff
let filePath = manager.videosPathForFileName("video.mov")
manager.saveData(data, toFilePath: filePath)
}).resume()
let image = UIImage(data: data)
dataManager.saveImage(image, fileName: manager.imagesPathForFileName("pic.jpg"))
let newImage = manager.getImageWithFileName("pic.jpg")
NSString *filePath = [manager documentsPathForFileName:@"array.json"];
[manager deleteFileAtFilePath:filePath]
These methods return a file path or URL for files stored in the application's documents or library folder.
- (NSString *)documentsPathForFileName:(NSString *)fileName;
- (NSString *)libraryPathForFileName:(NSString *)fileName;
Get the file paths for sub directories inside the documents folder.
- (NSString *)dataFilesPathForFileName:(NSString *)fileName;
- (NSString *)imagesPathForFileName:(NSString *)fileName;
- (NSString *)audioPathForFileName:(NSString *)fileName;
- (NSString *)videosPathForFileName:(NSString *)fileName;
- (NSURL *)videosURLForFileName:(NSString *)fileName;
- (BOOL)fileExistsInDocuments:(NSString *)fileName;
- (BOOL)dataFileExistsInDocuments:(NSString *)fileName;
- (BOOL)imageExistsInDocuments:(NSString *)fileName;
- (BOOL)audioFileExistsInDocuments:(NSString *)fileName;
- (BOOL)videoExistsInDocuments:(NSString *)fileName;
- (BOOL)videoURLExistsInDocuments:(NSURL *)url;
- (BOOL)deleteFileAtFilePath:(NSString *)filePath;
- (BOOL)deleteFileAtFileURL:(NSURL *)url;
- (BOOL)saveData:(NSData *)data toFilePath:(NSString *)filePath;
- (BOOL)saveData:(NSData *)data toDocumentsFile:(NSString *)fileName;
- (BOOL)saveObject:(id)object toFilePath:(NSString *)filePath;
- (BOOL)saveObject:(id)object toDocumentsFile:(NSString *)fileName;
- (id)fetchJSONFromDocumentsFilePath:(NSString *)filePath;
- (id)fetchJSONFromDocumentsFilepath:(NSString *)filePath error:(NSError **)error;
- (id)fetchJSONFromDocumentsDataFileName:(NSString *)fileName;
Image utilities.
- (BOOL)saveImage:(UIImage *)image fileName:(NSString *)fileName;
- (UIImage *)getImageWithFileName:(NSString *)fileName;
+ (DADataManager)sharedManager;