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

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

Is there a safer way to create a directory if it does not exist?

You can actually skip the if, even though Apple’s docs say that the directory must not exist, that is only true if you are passing withIntermediateDirectories:NO That puts it down to one call. The next step is to capture any errors: NSError * error = nil; [[NSFileManager defaultManager] createDirectoryAtPath:bundlePath withIntermediateDirectories:YES attributes:nil error:&error]; if (error != … Read more

NSFileManager.defaultManager().fileExistsAtPath returns false instead of true

(The code in this answer has been updated for Swift 3 and later.) Apparently your path variable is a NSURL (describing a file path). To get the path as a string, use the path property, not absoluteString: let exists = FileManager.default.fileExists(atPath: path.path) absoluteString returns the URL in a string format, including the file: scheme etc. … Read more

Delete files from directory inside Document directory?

In case anyone needs this for the latest Swift / Xcode versions: here is an example to remove all files from the temp folder: Swift 2.x: func clearTempFolder() { let fileManager = NSFileManager.defaultManager() let tempFolderPath = NSTemporaryDirectory() do { let filePaths = try fileManager.contentsOfDirectoryAtPath(tempFolderPath) for filePath in filePaths { try fileManager.removeItemAtPath(tempFolderPath + filePath) } } … Read more

How to overwrite a file with NSFileManager when copying?

If you can’t/don’t want to keep the file contents in memory but want an atomic rewrite as noted in the other suggestions, you can first copy the original file to a temp directory to a unique path (Apple’s documentation suggests using a temporary directory), then use NSFileManager’s -replaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error: According to the reference documentation, this method … Read more