Mac App Store - can't save a file

只谈情不闲聊 提交于 2019-12-11 08:04:48

问题


Are there any restrictions as far as saving files when you distribute an app over the Mac App Store?

If I compile and run my app from my computer it works fine - it saves the configuration.

However the version that was downloaded over the Mac App Store is not saving the configuration. I don't even see the config file. Anyone knows what is going on?

This is the code that saves the config:

-(void)saveConfig:(id)plist {

    NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingString: CONFIG_FILE_NAME];

    NSData *xmlData;
    NSString *error;

    xmlData = [NSPropertyListSerialization dataFromPropertyList: plist
        format: NSPropertyListXMLFormat_v1_0
        errorDescription: &error];
    if(xmlData)
    {
        if (![xmlData writeToFile:path atomically:YES])
            NSLog(@"Failed to write the config file onto the hard drive");
    }
    else
    {
        NSLog(error);
    }
}

回答1:


You cannot write files to the application bundle directory if you’re targeting the Mac App Store. The bundle is supposed to be immutable.

Consider saving your configuration with NSUserDefaults or, if you truly need a separate file, the officially recommended location is (~)/Library/Application Support. Matt Gallagher wrote a nice post called Finding or creating the application support directory in which he provides a solution that uses standard NSApplicationSupportDirectory followed by the executable name.




回答2:


Generally, you should assume that your application's assets are read-only. This is true in general, not just for the app store.

If you want to save user settings as a property list, use NSUserDefaults instead of modifying files inside the application. This will "just do the right thing", which is to save preferences to ~/Library/Preferences.



来源:https://stackoverflow.com/questions/6053551/mac-app-store-cant-save-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!