iOS9 Swift File Creating NSFileManager.createDirectoryAtPath with NSURL

I figured this one out. createDirectoryAtPath() is unable to process a path with the “file://” prefix. To get a path without the prefix you must use path() or relativePath(). let documentsPath = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]) let logsPath = documentsPath.URLByAppendingPathComponent(“logs”) do { try NSFileManager.defaultManager().createDirectoryAtPath(logsPath.path!, withIntermediateDirectories: true, attributes: nil) } catch let error as NSError { … Read more

Get directory contents in date modified order

nall’s code above pointed me in the right direction, but I think there are some mistakes in the code as posted above. For instance: Why is filesAndProperties allocated using NMutableDictonary rather than an NSMutableArray? NSDictionary* properties = [[NSFileManager defaultManager] attributesOfItemAtPath:NSFileModificationDate error:&error]; The code above is passing the wrong parameter for attributesOfItemAtPath – it should be … Read more

How to use iCloud to store and sync app files

What “works” for me is just simple: NSFileManager *fm = [NSFileManager defaultManager]; NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil]; if (ubiq == nil) { return NO; } NSError *theError = nil; [fm setUbiquitous:true itemAtURL:backupUrl destinationURL:[[ubiq URLByAppendingPathComponent:@”Documents” isDirectory:true] URLByAppendingPathComponent:backupName] error:&theError]; Apple says to call on the non-UI thread. Having the files “moved”. You can query for them … Read more

iphone – how to check if the file is a directory , audio , video or image?

Sample Code : NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSArray *directory = [[NSFileManager defaultManager] directoryContentsAtPath: documentsDirectory]; BOOL isDirectory; for (NSString *item in directory){ BOOL fileExistsAtPath = [[NSFileManager defaultManager] fileExistsAtPath:item isDirectory:&isDirectory]; if (fileExistsAtPath) { if (isDirectory) { //It’s a Directory. } } if ([[item pathExtension] isEqualToString:@”png”]) { //This is Image File … Read more

NSFileManager delete contents of directory

E.g. by using a directory enumerator: NSFileManager *fileManager = [[NSFileManager alloc] init]; NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtPath:path]; NSString *file; while (file = [enumerator nextObject]) { NSError *error = nil; BOOL result = [fileManager removeItemAtPath:[path stringByAppendingPathComponent:file] error:&error]; if (!result && error) { NSLog(@”Error: %@”, error); } } Swift let fileManager = NSFileManager.defaultManager() let enumerator = fileManager.enumeratorAtURL(cacheURL, … Read more

Save file in document directory in swift 3?

Please think the other way round. URL is the recommended way to handle file paths because it contains all convenience methods for appending and deleting path components and extensions – rather than String which Apple has removed those methods from. You are discouraged from concatenating paths like path = path + name. It’s error-prone because … Read more

iOS: How do you find the creation date of a file?

This code actually returns the good creation date to me: NSFileManager* fm = [NSFileManager defaultManager]; NSDictionary* attrs = [fm attributesOfItemAtPath:path error:nil]; if (attrs != nil) { NSDate *date = (NSDate*)[attrs objectForKey: NSFileCreationDate]; NSLog(@”Date Created: %@”, [date description]); } else { NSLog(@”Not found”); } Are you creating the file inside the App? Maybe that’s where the … Read more