Create nested directory in Application Support in Swift - ios

Trying to create a nested directory within Application Support and it's failing. The error message I get is "You don’t have permission to save the file 'folder1' in the folder 'testApp'.
let path = getApplicationSupportDirectory()
let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as! String
let folder = path.appendingPathComponent("\(appName)/folder1", isDirectory: true)
print("[ERR]: Folder location: \(folder.relativePath)")
if !FileManager.default.fileExists(atPath: folder.relativePath) {
do {
try FileManager.default.createDirectory(atPath: folder.relativeString, withIntermediateDirectories: true, attributes: nil)
} catch {
print("[ERR]: \(error.localizedDescription)")
}
}
Folder location outputs the correct location and it appears to create the first directory within "appName".

Please try this code, it's supposed to work.
It uses the API of FileManager to create the Application Support folder if it does not exist.
do {
let applicationSupportFolderURL = try FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as! String
let folder = applicationSupportFolderURL.appendingPathComponent("\(appName)/folder1", isDirectory: true)
print("[ERR]: Folder location: \(folder.path)")
if !FileManager.default.fileExists(atPath: folder.path) {
try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true, attributes: nil)
}
} catch { print(error) }

Related

When should we use FileManager.default.urls as opposed to NSSearchPathForDirectoriesInDomains? [duplicate]

This question already has an answer here:
UIImage(contentsOfFile:) returning nil despite file existing in caches directory [duplicate]
(1 answer)
Closed 1 year ago.
Currently, if I want to create a directory hierarchy in Document directory, I would perform the following
Using NSSearchPathForDirectoriesInDomains (Works fine)
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
let documentUrl1 = URL(string: documentsDirectory)!
//
// /Users/yccheok/Library/Developer/...
//
print("documentUrl1 -> \(documentUrl1)")
let dataPath = documentUrl1.appendingPathComponent("SubFolder1").appendingPathComponent("SubFolder2")
print("dataPath.absoluteString -> \(dataPath.absoluteString)")
if !FileManager.default.fileExists(atPath: dataPath.absoluteString) {
do {
try FileManager.default.createDirectory(atPath: dataPath.absoluteString, withIntermediateDirectories: true, attributes: nil)
print("Folder creation done!")
} catch {
print(error.localizedDescription)
}
}
But, if I use the following
Using FileManager.default.urls (Not working)
let documentUrl0 = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
//
// file:///Users/yccheok/Library/Developer/...
//
print("documentUrl0 -> \(documentUrl0)")
let dataPath = documentUrl0.appendingPathComponent("SubFolder1").appendingPathComponent("SubFolder2")
print("dataPath.absoluteString -> \(dataPath.absoluteString)")
if !FileManager.default.fileExists(atPath: dataPath.absoluteString) {
do {
try FileManager.default.createDirectory(atPath: dataPath.absoluteString, withIntermediateDirectories: true, attributes: nil)
print("Folder creation done!")
} catch {
print(error.localizedDescription)
}
}
The following error will be printed
You can’t save the file “SubFolder2” because the volume is read only.
I was wondering, under what use case, that FileManager.default.urls will be useful? Thanks.
URL(string and absoluteString are the wrong APIs to work with file system paths.
Your code accidentally works because URL(string applied to a path creates a path rather than a valid URL, and absoluteString applied to a path returns the path because there is no scheme (file://).
However you are strongly discouraged from doing that. A file system URL must be created with URL(fileURLWithPath and you can get the path with the path property.
You are encouraged to use the FileManager API because it provides a better error handling and it provides also the URL related API which is preferred over the string path API.
This is the correct FileManager way to create the directory if it doesn't exist
let fm = FileManager.default
do {
let documentUrl = try fm.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
//
// file:///Users/yccheok/Library/Developer/...
//
print("documentUrl -> \(documentUrl)")
let dataURL = documentUrl.appendingPathComponent("SubFolder1").appendingPathComponent("SubFolder2")
print("dataPath.path -> \(dataURL.path)")
if !fm.fileExists(atPath: dataURL.path) {
try fm.createDirectory(at: dataURL, withIntermediateDirectories: true, attributes: nil)
print("Folder creation done!")
}
} catch { print(error) }

Errors when writing to Application Support directory

I've seen this question asked a dozen times on here and yet the solution do not seem to solve my situation.
I have some content that needs to be stored in the 'Application Support' directory, but I continue receiving errors when trying to write there.
func assetFilesDirectory(asset: Asset, shouldCreate: Bool) -> URL? {
do {
let applicationSupportFolderURL = try FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let folder = applicationSupportFolderURL.appendingPathComponent("\(asset)")
if !FileManager.default.fileExists(atPath: folder.path) {
if shouldCreate {
try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true, attributes: nil)
} else {
return nil
}
}
return folder
} catch {
print(error)
return nil
}
}
When this code runs, I keep getting the following error:
"You don’t have permission to save the file “asset-backgrounds” in the folder “Application Support”."
I've tried toggling the value for the create parameter in the following line, but I get the same error with that too, just referencing the Application Support directory and Library instead.
let applicationSupportFolderURL = try FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
I haven't seen any reason why this should make any difference API-wise, but as information, I am running this code on the tvOS platform (physical device). There is no information indicating that Application Support should be permission-restricted on tvOS though.
I replaced your Asset (whatever that is) with a string and it worked fine:
func assetFilesDirectory(name: String, shouldCreate: Bool) -> URL? {
do {
let applicationSupportFolderURL = try FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let folder = applicationSupportFolderURL.appendingPathComponent(name)
if !FileManager.default.fileExists(atPath: folder.path) {
if shouldCreate {
try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true, attributes: nil)
print("ok")
} else {
return nil
}
}
return folder
} catch {
print(error)
return nil
}
}
I called
let url = self.assetFilesDirectory(name: "test", shouldCreate: true)
print(url)
and I got back "ok" and the URL file:///blahblahblah/Library/Application%20Support/test which is just what one would expect.

Copy all the bundle resources to a separate folder inside the Documents directory

What is the correct way of copying all the files contained in the Bundle (not the [NSBundle mainBundle]) and putting them to a newly created directory inside the Documents directory?
You need to copy the items one by one, it's fairly straightforward.
let bundle: Bundle = ... // Whatever bundle you want to copy from
guard let resourceURL = bundle.resourceURL else { return }
let fileManager = FileManager.default
do {
let documentsDirectory = try fileManager.url(for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: false)
let destination = documentsDirectory.appendingPathComponent("BundleResourcesCopy", isDirectory: true)
var isDirectory: ObjCBool = false
if fileManager.fileExists(atPath: destination.path, isDirectory: &isDirectory) {
assert(isDirectory.boolValue)
} else {
try fileManager.createDirectory(at: destination, withIntermediateDirectories: false)
}
let resources = try fileManager.contentsOfDirectory(at: resourceURL, includingPropertiesForKeys: nil)
for resource in resources {
print("Copy \(resource) to \(destination.appendingPathComponent(resource.lastPathComponent))")
try fileManager.copyItem(at: resource,
to: destination.appendingPathComponent(resource.lastPathComponent))
}
} catch {
print(error)
}
Depending on the size of the bundle this could take some time to perform, so you may want to perform this on a background thread.

How to make subfolder inside a folder

I want to save images to documents/images/AET/1.jpg but so far I was able to save to documents folder only.
I wrote code
let fileManager = FileManager.default
let paths = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("images")
try! fileManager.createDirectory(atPath: paths, withIntermediateDirectories: true, attributes: nil)
fileManager.changeCurrentDirectoryPath(paths)
let image = imagePic// UIImage(named: "apple.jpg")
print(paths)
let paths3 = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("/\(chkString!).jpg")
print("chkString=\(chkString!)")
if !fileManager.fileExists(atPath: paths){
try! fileManager.createDirectory(atPath: paths3, withIntermediateDirectories: true, attributes: nil)
}else{
print("Already dictionary created.")
}
let imageData = UIImageJPEGRepresentation(image, 0.5)
// let pathToDatabase = paths3.appending("/\(chkString)")
// let pathToDatabase = paths3.appendingPathComponent("/\(chkString)")
fileManager.createFile(atPath: paths3 as String, contents: imageData, attributes: nil)
print("imagePath = \(paths3)")
The main issue is that you cannot create a directory passing a file path including the file name (/../AET/1.jpg). You need to specify a folder path without the file name.
Generally it's highly recommended to use the URL related API.
This example uses the URL related API, more descriptive variable names and the API of Data to write the image data to disk.
let chkString : String? = "AET"
let fileManager = FileManager.default
do {
let documentDirectoryURL = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let imagesFolderURL = documentDirectoryURL.appendingPathComponent("images/\(chkString!)")
let imageURL = imagesFolderURL.appendingPathComponent("1.jpg")
let image = imagePic// UIImage(named: "apple.jpg")
if !fileManager.fileExists(atPath: imagesFolderURL.path){
try fileManager.createDirectory(at: imagesFolderURL, withIntermediateDirectories: true, attributes: nil)
} else {
print("Already dictionary created.")
}
let imageData = UIImageJPEGRepresentation(image, 0.5)
try imageData!.write(to: imageURL)
print("imagePath =", imageURL.path)
} catch {
print(error)
}
let fileManager = FileManager.default
let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let path = "\(documentPath[0])/images/AET"
if !fileManager.fileExists(atPath: path) {
try! fileManager.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil)
} else {
print("Directroy is already created")
}
You can print the path and check through terminal it will give the directory that you want.
Use this:
let fileManager = FileManager.default
let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
print(documentPath)
let imageFolder = documentPath.appending("/images/AET")
if !fileManager.fileExists(atPath: imageFolder) {
do{
try fileManager.createDirectory(atPath: imageFolder, withIntermediateDirectories: true, attributes: nil)
}
catch (let error){
print("Failed to create Directory: \(error.localizedDescription)")
}
}
You should create subfolders like "/images/AET".

Create Directory in Swift 3.0

I am a new student in 9th grade learning swift, creating a school project .
I am trying to create a directory where I want to save a scanned file into pdf format.
While creating directory I am getting error below.
Error 1:
Cannot use instance member 'filemgr' within property initializer; property initializers run before 'self' is available.
Error 2:
Expected declaration
Code:
let filemgr = FileManager.default
let dirPaths = filemgr.urls(for: .documentDirectory, in: .userDomainMask)
let docsURL = dirPaths[0]
let newDir = docsURL.appendingPathComponent("data").path
do{
try filemgr.createDirectory(atPath: newDir,withIntermediateDirectories: true, attributes: nil)
} catch {
print("Error: \(error.localizedDescription)")
}
Please assist me in resolving this issue.
Thanks.
Please use this code:
Swift 5.0,
Swift 4.0 And
Swift 3.0
let DocumentDirectory = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])
let DirPath = DocumentDirectory.appendingPathComponent("FOLDER_NAME")
do
{
try FileManager.default.createDirectory(atPath: DirPath!.path, withIntermediateDirectories: true, attributes: nil)
}
catch let error as NSError
{
print("Unable to create directory \(error.debugDescription)")
}
print("Dir Path = \(DirPath!)")
For Swift 4.0
Please use this
let fileManager = FileManager.default
if let tDocumentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first {
let filePath = tDocumentDirectory.appendingPathComponent("\(FOLDER_NAME)")
if !fileManager.fileExists(atPath: filePath.path) {
do {
try fileManager.createDirectory(atPath: filePath.path, withIntermediateDirectories: true, attributes: nil)
} catch {
NSLog("Couldn't create document directory")
}
}
NSLog("Document directory is \(filePath)")
}
For Swift 4.0, I created the following extension off of URL that allows for the creation of a folder off of the documents directory within the application.
import Foundation
extension URL {
static func createFolder(folderName: String) -> URL? {
let fileManager = FileManager.default
// Get document directory for device, this should succeed
if let documentDirectory = fileManager.urls(for: .documentDirectory,
in: .userDomainMask).first {
// Construct a URL with desired folder name
let folderURL = documentDirectory.appendingPathComponent(folderName)
// If folder URL does not exist, create it
if !fileManager.fileExists(atPath: folderURL.path) {
do {
// Attempt to create folder
try fileManager.createDirectory(atPath: folderURL.path,
withIntermediateDirectories: true,
attributes: nil)
} catch {
// Creation failed. Print error & return nil
print(error.localizedDescription)
return nil
}
}
// Folder either exists, or was created. Return URL
return folderURL
}
// Will only be called if document directory not found
return nil
}
}
If the desired folder does not exist, it will create it. Then, assuming the folder exists, it returns the URL back to the user. Otherwise, if it fails, then nil is returned.
For example, to create the folder "MyStuff", you would call it like this:
let myStuffURL = URL.createFolder(folderName: "MyStuff")
This would return:
file:///var/mobile/Containers/Data/Application/4DE0A1C0-8629-47C9-87D7-C2B4F3A16D24/Documents/MyStuff/
You can also create nested folders with the following:
let myStuffHereURL = URL.createFolder(folderName: "My/Stuff/Here")
Which gives you:
file:///var/mobile/Containers/Data/Application/4DE0A1C0-8629-47C9-87D7-C2B4F3A16D24/Documents/My/Stuff/Here/
You are getting this because you are assigning value to newDir instance at wrong place.
I wrote your code in viewDidLoad and it works perfectly.
For Swift 5 and up Version
let documentDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let directoryURL = documentDirectoryURL.appendingPathComponent("FolderName", isDirectory: true)
if FileManager.default.fileExists(atPath: directoryURL.path) {
print(directoryURL.path)
} else {
do {
try FileManager.default.createDirectory(at: directoryURL, withIntermediateDirectories: true, attributes: nil)
print(directoryURL.path)
} catch {
print(error.localizedDescription)
}

Resources