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

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

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 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

Delete specified file from document directory

I checked your code. It’s working for me. Check any error you are getting using the modified code below – (void)removeImage:(NSString *)filename { NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *filePath = [documentsPath stringByAppendingPathComponent:filename]; NSError *error; BOOL success = [fileManager removeItemAtPath:filePath error:&error]; if (success) { UIAlertView *removedSuccessFullyAlert = [[UIAlertView … Read more

What is the documents directory (NSDocumentDirectory)?

Your app only (on a non-jailbroken device) runs in a “sandboxed” environment. This means that it can only access files and directories within its own contents. For example Documents and Library. See the iOS Application Programming Guide. To access the Documents directory of your applications sandbox, you can use the following: iOS 8 and newer, … Read more

How to find NSDocumentDirectory in Swift?

Apparently, the compiler thinks NSSearchPathDirectory:0 is an array, and of course it expects the type NSSearchPathDirectory instead. Certainly not a helpful error message. But as to the reasons: First, you are confusing the argument names and types. Take a look at the function definition: func NSSearchPathForDirectoriesInDomains( directory: NSSearchPathDirectory, domainMask: NSSearchPathDomainMask, expandTilde: Bool) -> AnyObject[]! directory … Read more