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

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

How to create directory using Swift code (NSFileManager)

Swift 5.0 let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) let documentsDirectory = paths[0] let docURL = URL(string: documentsDirectory)! let dataPath = docURL.appendingPathComponent(“MyFolder”) if !FileManager.default.fileExists(atPath: dataPath.path) { do { try FileManager.default.createDirectory(atPath: dataPath.path, withIntermediateDirectories: true, attributes: nil) } catch { print(error.localizedDescription) } } Swift 4.0 let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true) let documentsDirectory: AnyObject = paths[0] as AnyObject … Read more

Testing file existence using NSURL

NSURL does have this method: – (BOOL)checkResourceIsReachableAndReturnError:(NSError **)error Which “Returns whether the resource pointed to by a file URL can be reached.” NSURL *theURL = [NSURL fileURLWithPath:@”/Users/elisevanlooij/nonexistingfile.php” isDirectory:NO]; NSError *err; if ([theURL checkResourceIsReachableAndReturnError:&err] == NO) [[NSAlert alertWithError:err] runModal];

tech