Ios: Can't Save File to 'Application Support' Folder, But Can to 'Documents'

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;

How to write to local Application Support directory on OS X

The directory /Library/Application Support/ belongs to root. You can see that in a Terminal by typing:

$ ls -al /Library | grep Appl*

drwxr-xr-x 15 root admin 480 Jan 4 16:10 Application Support

To write to that directory your App needs root privileges. Refer to Apple Documentation to securely implement this. The Apple documentation mentions authopen which seems reasonable to create a file in the support folder at the first run of your App.

Saving a file inside app's main directory

Long story short:

No, you can't. For obvious reasons it's impossible to write into the application bundle.

iOS : I can't write my file

You ask:

Why?

It's that path because that's where the file is stored.

On the device it will be different.

Note that you can't write a file to that folder anyway. You should perhaps instead write to your app's documents folder:

//Get the documents folder
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];

//Get the final path of your file.
NSString *finalPath = @"myfile.txt"];
//And actually write that file.
[[NSFileManager defaultManager] createFileAtPath:finalPath contents:/*fileData as NSData*/ attributes:nil];

Permission Denied when trying to create a directory in Application Support

The problem is in this line

directory = URL(fileURLWithPath: directory).appendingPathComponent("IAP").absoluteString

This return filePath, while below function required simple path of directory.

fileManager.createDirectory(atPath: directory, withIntermediateDirectories: true, attributes: nil)

So either change this line

directory = URL(fileURLWithPath: directory).appendingPathComponent("IAP").absoluteString

with this line

directory = directory + "/IAP"

OR
change same line with below line

let url = URL(fileURLWithPath: directory).appendingPathComponent("IAP")

and use this function to create directory

fileManager.createDirectory(at: directory, withIntermediateDirectories: true, attributes: nil)

Hope this helps you.



Related Topics



Leave a reply



Submit