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

Finding file’s size

Try this; NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&attributesError]; NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize]; long long fileSize = [fileSizeNumber longLongValue]; Note that the fileSize won’t necessarily fit in an integer (especially a signed one) although you could certainly drop to a long for iOS as you’ll never exceed that in reality. The example uses long … 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

Is there any way to see the file system on the iOS simulator?

UPDATE: Since iOS 8: ~/Library/Developer/CoreSimulator/Devices The location used to be: ~/Library/Application Support/iPhone Simulator It had directories for all models of simulators (4.0, 4.1, 5.0, etc) you have ever run, go to the one you are running from in Xcode. Once in a folder, go to Applications, choose the Finder option that shows date for files, … Read more

How to check if a file exists in Documents folder?

Swift 3: let documentsURL = try! FileManager().url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) … gives you a file URL of the documents directory. The following checks if there’s a file named foo.html: let fooURL = documentsURL.appendingPathComponent(“foo.html”) let fileExists = FileManager().fileExists(atPath: fooURL.path) Objective-C: NSString* documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; NSString* foofile = [documentsPath stringByAppendingPathComponent:@”foo.html”]; BOOL … Read more

Storing images locally on an iOS device

The simplest way is to save it in the app’s Documents directory and save the path with NSUserDefaults like so: NSData *imageData = UIImagePNGRepresentation(newImage); NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *imagePath =[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@”%@.png”,@”cached”]]; NSLog(@”pre writing to file”); if (![imageData writeToFile:imagePath atomically:NO]) { NSLog(@”Failed to cache image data to … Read more

Create a folder inside documents folder in iOS apps

I do that the following way: NSError *error; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@”/MyFolder”]; if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder