0

my code is this. And the value of ret is always NO, I think the write path is not allowed. But where I can store my information on MAC OS X for my app? Can you help me to find the right path to store my app's setting? Thank you very much~ :)

NSString* writePath = [[[NSBundle mainBundle] resourcePath]
                        stringByAppendingPathComponent:@"Brain.plist"];

NSLog(@"%@",writePath);
NSMutableDictionary* dictForRet = [[NSMutableDictionary alloc]init];
NSNumber* applicationNumber = [[NSNumber alloc]initWithInt:0];
NSMutableDictionary* root = [[NSMutableDictionary alloc]init];
NSArray* propertyArray = [[NSArray alloc]initWithObjects:kPropertyArrayApplicationPath,kPropertyArrayApplicationCS, nil];
NSMutableDictionary* brain = [[NSMutableDictionary alloc]init];
[root setObject:propertyArray forKey:kPropertyArrayName];
[dictForRet setObject:applicationNumber forKey:kPropertyKeyApplicationNumber];
[dictForRet setObject:root forKey:kPropertyArrayDictName];
[dictForRet setObject:brain forKey:kPropertyDictBrainName];
NSLog(@"%@",dictForRet);
ret = [dictForRet writeToFile:writePath atomically:YES];
NSLog(@"%d",ret);
RetVal
  • 107
  • 3
  • 10

2 Answers2

2

The resource path refers to the Resources/ directory in your app bundle. You do not want to write to this path, which is usually not allowed (especially in case of a sandboxed app).

To get paths to standard locations, like the Application Support directory use NSSearchPathForDirectoriesInDomains.

You're probably looking for something like:

NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);

This will return an (array of size 1) Application Support URL, automatically picking the right path if your app is sandboxed.

Joris Kluivers
  • 11,894
  • 2
  • 48
  • 47
  • I am sorry, the app is not sandboxed, running on LION. And How can my class get my app's application support directory full path? such as "/Users/xxxx/Library/Application Support/myApplication/" – RetVal Apr 20 '12 at 14:53
  • Lion apps can be (and should be if released through the app store) sandboxed. However, if you use NSSearchPathForDirectoriesInDomains it will return the correct directory for both types of applications, either sandboxed or not (that's the advantage of using system functions). Just append your application name to the path returned by this function. – Joris Kluivers Apr 20 '12 at 14:59
  • I am already try to run this code, and the return path is "/Users/xxxx/Library/Application Support".And I just start to learn to programming on os x, so I am not family with the system functions, can you give some keywords that can help me to find the documents or simple sample code. Thank you again. – RetVal Apr 20 '12 at 15:07
  • That's all you need right. Combining that with the code form your opening post: `[path stringByAppendingPathComponent:@"MyApp"]` should result in your app path. You might need to create that directory using `-[NSFileManager createDirectoryAtPath:withIntermediateDirectories:attributes:error:]`because it doesn't exist by default. After that just append `Brains.plist` to that path and you've got a writable location for your plist file. – Joris Kluivers Apr 20 '12 at 15:16
  • I think that way is better.`NSString *appName = [[[NSBundle mainBundle] infoDictionary] objectForKey: (NSString *)kCFBundleNameKey]; if (nil == appName) appName = [[NSProcessInfo processInfo] processName];` And I find the problem that the directory is not existing by default. And if I should store all my need information in that directory? In iOS, is the setting files is saved in the app bundle?Thank you very much. – RetVal Apr 20 '12 at 15:31
  • Yes, that is a good way to find the application name. You should probably create the directory manually. And same on iOS, you cannot write to the app bundle. An alternative on iOS would be to save your data in locations returned when passing `NSDocumentDirectory`. – Joris Kluivers Apr 20 '12 at 15:36
  • 1
    @RetVal: Even if you have not sandboxed the application, there are still valid cases in which it will not be writable. You cannot assume it will be writable. It's best to not even try it; simply write somewhere that will be writable. – Peter Hosey Apr 20 '12 at 19:40
0

You can't write to the current app's bundle.

Instead, you might stuff it in your sandbox, in ~/Library/Application Support/YOUR_APP/, or in a user-specified location.

justin
  • 104,054
  • 14
  • 179
  • 226
  • But how can I change the setting of the default plist files? Should I rewrite all setting files to the path "~/Library/Application Support/YOUR_APP/"? And How I can get the location by cocoa code.PS:the app is not sandboxed. – RetVal Apr 20 '12 at 14:53
  • you use the bundled file as a reference, and save a copy of it elsewhere (e.g. "~/Library/Application Support/YOUR_APP/"). in some cases, you may want to store some information in `NSUserDefaults`. – justin Apr 20 '12 at 15:23
  • 1
    The Application Support folder is not a sandbox. In a sandboxed app, the sandbox container directory is in a different location (within Library/Containers, IIRC), and the Application Support directory is inside that. – Peter Hosey Apr 20 '12 at 19:41