iOS: Can't save file to 'Application Support' folder, but can to 'Documents'
Unlike the Documents
directory, the Application Support
directory does not exist in the app's sandbox by default. You need to create it before you can use it.
And a much simpler way to get a reference to the directory is:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *appSupportDirectory = paths.firstObject;
Programmatically get path to Application Support folder
This is outdated, for current best practice use FileManager.default.urls(for:in:)
as in the comment by @andyvn22 below.
the Best practice is to use NSSearchPathForDirectoriesInDomains
with NSApplicationSupportDirectory
as "long winded" as it may be.
Example:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *applicationSupportDirectory = [paths firstObject];
NSLog(@"applicationSupportDirectory: '%@'", applicationSupportDirectory);
NSLog output:
applicationSupportDirectory: '/Volumes/User/me/Library/Application Support'
Create nested directory in Application Support in Swift
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) }
Text File is not being created in applicationSupport folder in Swift
Apart from the wrong API absoluteString
the application support folder might not exist.
This is more reliable
let string = "password"
let encrypted = string.toHexString()
let fileManager = FileManager.default
do {
let applicationSupportDirectory = try fileManager.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let fileUrl = applicationSupportDirectory.appendingPathComponent(self.filePin)
let data = Data(encrypted.utf8)
try data.write(to: fileUrl)
print("The file was created")
} catch {
print(error, "File was not created")
}
unable to write to a file in AplicationSupport directory
The problem is that writeToURL will not create any missing directories in the path for you. If you call:
NSError *error;
[sharedFM createDirectoryAtURL:appDirectory withIntermediateDirectories:NO attributes:nil error:&error];
this will create the directory.
One question, why are you adding the bundleIdentifier into the path? The app support directory you get is already private to your app's sandbox. If you don't add this then you don't need to create the directory.
Objective C documents directory keeps changing on device
If you are storing paths in persistent storage with the intent to use that path at some date in the future, you should store only the path relative to the app's Documents folder and then whenever you need to access that resource, get the Documents folder path at runtime and append your relative path of your resource.
As an aside, I only see this changing sandbox path behavior when I run my app via Xcode. If I run the app directly from my device, the path is unchanged. Regardless, I'd always be inclined to use relative paths rather than fixed paths. I would never assume that the sandbox path couldn't change (e.g., if app is backed up and restored later, I wouldn't assume that the paths are unchanged).
iPhone simulator folder not in Application Support
Xcode versions 3.x-5.x
Simulator usually is installed together with SDK so most probably you should look (e.g., if you installed Xcode 3.1 and SDK 4.3 in their default locations) within:
/Developer/Platforms/iPhoneSimulator.platform/Developer/Applications/iOS Simulator.app
Apps that are installed on your iPhone Simulator should be (if for example compiled for iOS 4.3) located in:
~/Library/Application Support/iPhone Simulator/4.3/Applications/[app GUID]
If you are looking for .sqlite database within Application, it should reside in:
~/Library/Application Support/iPhone Simulator/4.3/Applications/[app GUID]/Documents/[appname].sqlite
Xcode 6
The simulator devices are located in:
~/Library/Developer/CoreSimulator/Devices/
so your application data (e.g. application database in app Document folder) will be in
~/Library/Developer/CoreSimulator/Devices/[simulator device id]/data/Applications/[app GUID]/Documents/[appname.sqlite]
Related Topics
Does Cidetector for Other Barcode Types
How to Write a Flip Method in Swift
Nsurlsession Upload File to Server Swift
How to Insert UIlabel After UItextfield in UIalertcontroller
Add Assets to an Icloud Shared Photo Album Programmatically
Completion Handler in For/In Loop - Swift
Swift Ensembles Set Up & Ubiquitycontaineridentifier
Swift3 Different Font in The All of The UIview with Localization Each
Type Alias Declaration with Templates in Swift
Why My Arguments Are Being Blocked When Running a Shell Command
Swift: Gradient Splits on Rotation
How to Get The Range of The First Line in a String
Swift: Handling an Unexpected Nil Value, When Variable Is Not Optional
Social Framework Is Deprecated in iOS11
Why Does Editing a Textfield Throw a Nsinvalidargumentexception