可靠地获取 iOS 应用程序的“~/Library”路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3762941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting an iOS application's "~/Library" path reliably
提问by Ben Zotto
This Apple tech note:
此 Apple 技术说明:
http://developer.apple.com/library/ios/#qa/qa2010/qa1699.html
http://developer.apple.com/library/ios/#qa/qa2010/qa1699.html
suggests storing "internal" user documents in a subdirectory off of ~/Library
. But I can't find one of the pre-created search domains that would get me this. What's the best/most correct/least likely to ever break way of constructing this path?
建议将“内部”用户文档存储在~/Library
. 但是我找不到可以让我得到这个的预先创建的搜索域之一。构建这条路径的最佳/最正确/最不可能破坏的方式是什么?
Thanks.
谢谢。
回答by kennytm
The correct way is
正确的方法是
NSString* path;
path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];
However, [@"~/Library" stringByExpandingTildeInPath]
also works.
但是,[@"~/Library" stringByExpandingTildeInPath]
也有效。
Swift 3:
斯威夫特 3:
let path = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)[0]
回答by Vidhur
You can also try this:
你也可以试试这个:
[NSHomeDirectory() stringByAppendingString:@"/Library"]