Adding your directory <bundle_ID>. What is it?
I'm having trouble writing a file to the AppSupport folder in Xcode 5 for iOS 7. What am I trying to do:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *plistPath = [[paths lastObject] stringByAppendingPathComponent:@"somedata.plist"];
if(plistData) {
NSLog(@"PATH: %@", plistPath);
BOOL success = [plistData writeToFile:plistPath atomically:YES];
NSLog(@"SUCCESS: %hhd",success);
}
else {
NSLog(@"ERROR CREATING PLIST: %@",error);
}
And I get NO as output:
PATH: /var/mobile/Applications/40954....7E3C/Library/Application Support/somedata.plist
SUCCESS: 0
Apple Documentation says:
Use the Application Support directory constant NSApplicationSupportDirectory,
appending your <bundle_ID> for: …
What does adding your package_ID mean? Maybe there is another way that I should use? NSDocumentDirectory is not suitable for me, because this is the place for user files.
+4
1 answer
Saving is a NSApplicationSupportDirectorybit trickier than
Follow this sample Apple code to write files to this directory.
- (NSURL*)applicationDirectory
{
NSString* bundleID = [[NSBundle mainBundle] bundleIdentifier];
NSFileManager*fm = [NSFileManager defaultManager];
NSURL* dirPath = nil;
// Find the application support directory in the home directory.
NSArray* appSupportDir = [fm URLsForDirectory:NSApplicationSupportDirectory
inDomains:NSUserDomainMask];
if ([appSupportDir count] > 0)
{
// Append the bundle ID to the URL for the
// Application Support directory
dirPath = [[appSupportDir objectAtIndex:0] URLByAppendingPathComponent:bundleID];
//Modified code to write your plist file to the Application support dir
NSString *plistPath = [dirPath stringByAppendingPathComponent:@"somedata.plist"];
//Assuming plistData is pre-populated ivar
//Else add your code to create plistData here
if(plistData) {
BOOL success = [plistData writeToFile:plistPath atomically:YES];
NSLog(@"SUCCESS: %hhd",success);
}
else {
NSLog(@"ERROR CREATING PLIST: %@",error);
}
}
}
+3