Getting path to users Library folder in OS X

痴心易碎 提交于 2019-12-22 04:35:08

问题


I need to open a NSSavePanel with the users Library folder as destination folder. Normally I would do this by entering ~/Library/ in [NSSavePanel beginSheetForDirectory].

This works fine as long as the application is not sandboxed. For sandboxed applications this will result in the NSSavePanel trying to access a folder inside the applications document "box".

I cannot refer to /Users/username/Library/ as I do not know the users username at runtime. So how do I link to this path in cocoa?


回答1:


Not sure if this will work on a sandboxed application but this is how I do it right now. This will return /User/TheirUserName

-(NSString *)homeDirectory
{
    return NSHomeDirectory();
}



回答2:


I'm not sure how sandboxing fits in with this, but you can find the user's library directory using:

NSArray* paths = NSSearchPathForDirectoriesInDomains( NSLibraryDirectory, NSUserDomainMask, YES );



回答3:


It depends what you are trying to achieve.

  • If the behavior is required by your application, then you can request a temporary exception entitlement when submitting the application to the Mac App Store. But sooner or later, you will have to find a solution to remove this exception.
  • If you want to access data that were previously stored in the ~/Library/ folder, you can define a migration strategy to move back the data into the sandbox.


来源:https://stackoverflow.com/questions/7993319/getting-path-to-users-library-folder-in-os-x

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