沙盒和NSBundle的区别

1 沙盒和NSBundle的区别

沙盒(NSHomeDirectory()) 是系统加载 app时,为 app 分配的存储空间。如本地数据库,文件存储;

NSBundle 是系统加载 app时,app 的可执行代码和这些代码需要的资源文件所在的目录;

下面打印出他们的地址。

注意:每次打印出的地址都是不同的

- (void)viewDidLoad {

[super viewDidLoad];

NSLog(@"bundle: %p", [[NSBundle mainBundle] infoDictionary]);

NSLog(@"home: %p", NSHomeDirectory());

NSLog(@"code: %p", self);

NSLog(@"bundle: %@", [[NSBundle mainBundle] bundlePath]);

NSLog(@"home: %@", NSHomeDirectory());

NSString *content = @"my file content";

NSError *error;

BOOL isRight = NO;

isRight = [content writeToFile:[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"file1.txt"]

atomically:YES

encoding:NSUTF8StringEncoding

error:&error];

if (!isRight) {

NSLog(@"write to bundle error: %@", error.localizedDescription);

}

isRight = [content writeToFile:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"file1.txt"]

atomically:YES

encoding:NSUTF8StringEncoding

error:&error];

if (!isRight) {

NSLog(@"write to home dir error: %@", error.localizedDescription);

}

}

模拟器中输出:

第 1 次运行:

2017-07-26 10:38:41.307 Harvest[2236:541410] bundle: 0x600000065380

2017-07-26 10:38:41.307 Harvest[2236:541410] home: 0x7fd6d65043d0

2017-07-26 10:38:41.307 Harvest[2236:541410] code: 0x7fd6d650d820

2017-07-26 10:38:41.308 Harvest[2236:541410] bundle: /Users/longhua/Library/Developer/CoreSimulator/Devices/2CB55BFD-8176-4ADC-99D6-5280D7BCCF1B/data/Containers/Bundle/Application/817CFB68-19FE-4D58-960F-68F72BE236B7/Harvest.app

2017-07-26 10:38:41.308 Harvest[2236:541410] home: /Users/longhua/Library/Developer/CoreSimulator/Devices/2CB55BFD-8176-4ADC-99D6-5280D7BCCF1B/data/Containers/Data/Application/E1D22141-A32F-468E-91D5-307BD82FA251

第 2 次运行:

2017-07-26 10:39:13.476 Harvest[2273:544438] bundle: 0x608000261040

2017-07-26 10:39:13.476 Harvest[2273:544438] home: 0x7f83b470cbb0

2017-07-26 10:39:13.476 Harvest[2273:544438] code: 0x7f83b450e460

2017-07-26 10:39:13.477 Harvest[2273:544438] bundle: /Users/longhua/Library/Developer/CoreSimulator/Devices/2CB55BFD-8176-4ADC-99D6-5280D7BCCF1B/data/Containers/Bundle/Application/05F27F47-8384-40A7-9FDF-451255E282B3/Harvest.app

2017-07-26 10:39:13.477 Harvest[2273:544438] home: /Users/longhua/Library/Developer/CoreSimulator/Devices/2CB55BFD-8176-4ADC-99D6-5280D7BCCF1B/data/Containers/Data/Application/D91EC197-590D-4538-9651-435056AB5D19

真机中输出:

第 1 次运行:

2017-07-26 10:33:04.426597+0800 Harvest[7963:2911220] bundle: 0x17007b8c0

2017-07-26 10:33:04.426826+0800 Harvest[7963:2911220] home: 0x17414e650

2017-07-26 10:33:04.426857+0800 Harvest[7963:2911220] code: 0x14de0ed10

2017-07-26 10:33:04.426885+0800 Harvest[7963:2911220] bundle: /var/containers/Bundle/Application/0FF63C1B-80CC-4C79-9119-3EABE8D61F14/Harvest.app

2017-07-26 10:33:04.427091+0800 Harvest[7963:2911220] home: /var/mobile/Containers/Data/Application/EA36910B-A24D-48BD-A657-561247429851

2017-07-26 10:33:04.446798+0800 Harvest[7963:2911220] write to bundle error: 您没有将文件“file1.txt”存储到文件夹“丰收app”中的权限。

第 2 次运行:

2017-07-26 10:35:51.724381+0800 Harvest[7969:2912283] bundle: 0x17007c7c0

2017-07-26 10:35:51.724596+0800 Harvest[7969:2912283] home: 0x174150490

2017-07-26 10:35:51.724627+0800 Harvest[7969:2912283] code: 0x145e0e4e0

2017-07-26 10:35:51.724655+0800 Harvest[7969:2912283] bundle: /var/containers/Bundle/Application/36A3A0D0-B007-45B1-8672-7A9195CEDBF5/Harvest.app

2017-07-26 10:35:51.724857+0800 Harvest[7969:2912283] home: /var/mobile/Containers/Data/Application/BDAA6308-C671-4022-B97C-F8EFE36CE746

2017-07-26 10:35:51.740230+0800 Harvest[7969:2912283] write to bundle error: 您没有将文件“file1.txt”存储到文件夹“丰收app”中的权限。

显示 Harvest.app 的包内容:

沙盒和NSBundle的区别_第1张图片

打开 NSHomeDirectory() 中的内容:

沙盒和NSBundle的区别_第2张图片

结果分析:

(1)bundle 中有个 info.plist 文件,是 app 的配置文件;沙盒的 Library / Preferences 中有个 com.god.harvest.plist 文件,它用于 UserDefault 存储;

另:使用 xcode > Devices 下载 app 的 container 内容,会看到主要就是 沙盒中的内容,再加上一个 AppDataInfo.plist(就是 info.plist).

(2)模拟器中,可以通过 writeToFile 向 沙盒 和 Bundle 中 写入文件;真机中是不能向 Bundle 中写入文件的;

官方文档中说明(地址:https://developer.apple.com/library/content/documentation/CoreFoundation/Conceptual/CFBundles/AccessingaBundlesContents/AccessingaBundlesContents.html#//apple_ref/doc/uid/10000123i-CH104-SW1):

• A package is any directory that the Finder presents to the user as if it were a single file.

• A bundle is a directory with a standardized hierarchical structure that holds executable code and the resources used by that code.

你可能感兴趣的:(沙盒和NSBundle的区别)