这两者都是iOS里获取本机存储路径的方式,整理相关内容分为简单版本和详细版本。
1.简单版本:
一个App创建后文件存储具体分类如上图,Bundle Container主要存储App需要的相关文件,这些文件是创建App时程序猿添加的;Data Container主要存储用户相关的文件。
NSBundle获取工程中.plist文件并读取数据
//NSBundle获取plist文件:
NSURL *bundleURL = [[NSBundle mainBundle] bundleURL];
NSURL *plistUrl = [bundleURL URLByAppendingPathComponent:@"plistFileName.plist"];
//获取NSDictionary数据
NSDictionary *myDictionary = [NSDictionary dictionaryWithContentsOfURL:plistUrl];
NSSearchPathForDirectoriesInDomains获取文件路径并创建文件
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"yourFileName.jpg"];
NSFileManager *manager = [NSFileManager defaultManager];
//创建文件
if (![manager fileExistsAtPath:path]) {
[manager createFileAtPath:path contents:nil attributes:nil];
}
2.详细版本
相关内容都是参考官网得到的https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html
2.1NSBundle
程序打包的相关资源文件会存放到.app的文件下,该文件只能读取不能写入,防止程序在打包生成后被更改。而相关路径的获取就是依靠NSBundle,实例如下:
NSURL *bundleURL = [[NSBundle mainBundle] bundleURL];
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSLog(@"bundleURL = %@\nbundlePath = %@", bundleURL, bundlePath);
打印的Log如下:
bundleURL = file:///Users/daobao777/Library/Developer/CoreSimulator/Devices/1493BDC2-10D8-47B2-8ED0-0A64C7BE3A4C/data/Containers/Bundle/Application/B217E31F-63DE-4F68-92E1-F0F0CA738F9A/MQTTClientTest.app/
bundlePath = /Users/daobao777/Library/Developer/CoreSimulator/Devices/1493BDC2-10D8-47B2-8ED0-0A64C7BE3A4C/data/Containers/Bundle/Application/B217E31F-63DE-4F68-92E1-F0F0CA738F9A/MQTTClientTest.app
获取到路径后用stringByAppendingString:@"文件名.xxx"
就可以得到文件路径,进行相关操作。
2.2NSSearchPathForDirectoriesInDomains
用户数据就是用这个方法得到路径的,该方法 NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory directory, NSSearchPathDomainMask domainMask, BOOL expandTilde)有三个参数。
第一个directory是设置想要获取的文件夹名称,比较常用的就是NSDocumentDirectory和NSCachesDirectory,分别是/Documents和/Library/Caches。用户数据一般都是选择保存在这两个目录下。Documents目录用来存储用户生成的内容,可以通过文件共享提供给用户,同时目录的内容会被iTunes和iCloud备份。而Caches目录用来存储不希望向用户公开的任何文件,该目录的内容不会被iTunes和iCloud备份。全部目录如下:
NSApplicationDirectory = 1, // supported applications (Applications)
NSDemoApplicationDirectory, // unsupported applications, demonstration versions (Demos)
NSDeveloperApplicationDirectory, // developer applications (Developer/Applications). DEPRECATED - there is no one single Developer directory.
NSAdminApplicationDirectory, // system and network administration applications (Administration)
NSLibraryDirectory, // various documentation, support, and configuration files, resources (Library)
NSDeveloperDirectory, // developer resources (Developer) DEPRECATED - there is no one single Developer directory.
NSUserDirectory, // user home directories (Users)
NSDocumentationDirectory, // documentation (Documentation)
NSDocumentDirectory, // documents (Documents)
NSCoreServiceDirectory, // location of CoreServices directory (System/Library/CoreServices)
NSAutosavedInformationDirectory API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 11, // location of autosaved documents (Documents/Autosaved)
NSDesktopDirectory = 12, // location of user's desktop
NSCachesDirectory = 13, // location of discardable cache files (Library/Caches)
NSApplicationSupportDirectory = 14, // location of application support files (plug-ins, etc) (Library/Application Support)
NSDownloadsDirectory API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)) = 15, // location of the user's "Downloads" directory
NSInputMethodsDirectory API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 16, // input methods (Library/Input Methods)
NSMoviesDirectory API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 17, // location of user's Movies directory (~/Movies)
NSMusicDirectory API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 18, // location of user's Music directory (~/Music)
NSPicturesDirectory API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 19, // location of user's Pictures directory (~/Pictures)
NSPrinterDescriptionDirectory API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 20, // location of system's PPDs directory (Library/Printers/PPDs)
NSSharedPublicDirectory API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 21, // location of user's Public sharing directory (~/Public)
NSPreferencePanesDirectory API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 22, // location of the PreferencePanes directory for use with System Preferences (Library/PreferencePanes)
NSApplicationScriptsDirectory NS_ENUM_AVAILABLE(10_8, NA) = 23, // location of the user scripts folder for the calling application (~/Library/Application Scripts/code-signing-id)
NSItemReplacementDirectory API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 99, // For use with NSFileManager's URLForDirectory:inDomain:appropriateForURL:create:error:
NSAllApplicationsDirectory = 100, // all directories where applications can occur
NSAllLibrariesDirectory = 101, // all directories where resources can occur
NSTrashDirectory API_AVAILABLE(macos(10.8), ios(11.0)) API_UNAVAILABLE(watchos, tvos) = 102 // location of Trash directory
第二个参数是路径的选择,有5种参数,一般用NSUserDomainMask获取用户路径,而NSAllDomainsMask是获取所有路径(目前4种)的文件,参数如下:
NSUserDomainMask = 1, // user's home directory --- place to install user's personal items (~)
NSLocalDomainMask = 2, // local to the current machine --- place to install items available to everyone on this machine (/Library)
NSNetworkDomainMask = 4, // publically available location in the local area network --- place to install items available on the network (/Network)
NSSystemDomainMask = 8, // provided by Apple, unmodifiable (/System)
NSAllDomainsMask = 0x0ffff // all domains: all of the above and future items
例如我要获取Caches文件夹,如果设置NSAllDomainsMask就会给出所有路径:
NSArray *s4 = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSAllDomainsMask, YES);
NSLog(@"s4 = %@", s4);
输出结果:
s4 = (
"/Users/daobao777/Library/Developer/CoreSimulator/Devices/1493BDC2-10D8-47B2-8ED0-0A64C7BE3A4C/data/Containers/Data/Application/DD8FD6BA-1A1F-4B4A-912C-5D3F5B0A1A9C/Library/Caches",
"/Library/Caches",
"/System/Library/Caches"
)
第三个参数是是否显示完整路径,也就是将“~”展开。
3.总结
结合官方文档和Xcode文档对NSBundle和NSSearchPathForDirectoriesInDomains进行分析,有什么不对的欢迎指出,谢谢~~ :)