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
Creating a 3X3 Grid with Auto Layout Constraints
Detecting Active Avaudiosessions on iOS Device
Using iOS Gamekit's "Bluetooth Bonjour" with Other Platforms
Bringing a Subview to Be in Front of All Other Views
How to Open Location Services Screen from Setting Screen
iOS 9 - "Attempt to Delete and Reload the Same Index Path"
How to Use Settitletextattributes:Forstate in Uibaritem
Uiactivityviewcontroller - Email and Twitter Sharing
How to Lock Portrait Orientation for Only Main View Using Swift
Change Color of Png in Buttons - iOS
Refresh Certain Row of Uitableview Based on Int in Swift
Improper Advertising Identifier [Idfa] Usage
Allow Unverified Ssl Certificates in Wkwebview
Align Text Using Drawinrect:Withattributes:
How to Use Autocorrection and Shortcut List in iOS8 Custom Keyboard
How to Make a Call Which Begin with * in Iphone
Return Lighter Color from Skcolor Using Hsl Lightness Factor