你的位置:首页 > 操作系统

[操作系统]iOS沙盒简单介绍


先简单介绍一下什么是沙盒:你可以简单理解成为一个目录,这个目录的改动不会对操作系统造成任何损失。(这里也有一点点介绍)

看看苹果的沙盒目录:

再附一张苹果官方的图

一个iOS app操作都是在自己的沙盒中进行的。

首先:

Deveices,里面是各种的模拟器设备。

然后随便找一个模拟器设备。里面的data是里面的数据。然后Container里面Data中有一个Application就是该设备的安装软件。

我们可以看到里面有好多个(就算我们Reset Content and Settings,也会有。因为里面有设备自带的软件,例如通讯录,地图等。)。

随便打开一个就可以看到里面某个程序包括三个目录:

Documents

Library:又包括Caches和Preferences

temp

其中的

Documents只有用户生成的文件、其他的数据以及程序程序不能创建的文件,这里的数据通过iCloud自动备份。我们录制的音频和拍照的图片都可以放到这里。

Library:可以重新下载或者重新生成的数据应该保存在Library的caches目录中。这里用于盛放缓存。

tmp:是临时使用的数据。iCloud不会自动备份这些文件,这些数据使用完后要随时删除,避免占用用户设备空间。

- (NSString *)documentDirectory {  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  return [paths objectAtIndex:0];}

通过上面这个方法可以得到:

/Users/zhanggui/Library/Developer/CoreSimulator/Devices/B6134687-CC10-40DE-BD9D-A2E5AD2D5C03/data/Containers/Data/Application/34361283-8619-481D-B8CB-B25EB4B787E2/Documents

也就是Documents的路径.

还可以得到通过下面的方法:

 NSString *s = NSHomeDirectory();

得到如下结果:

/Users/zhanggui/Library/Developer/CoreSimulator/Devices/B6134687-CC10-40DE-BD9D-A2E5AD2D5C03/data/Containers/Data/Application/08A6D627-1363-48B4-8AF8-A1ED361DC9D6

故名思议,我们通过NSHomeDirectory得到了该安装软件的目录。(里面就包括Documents/Library/temp)

苹果官方介绍的是返回用户的或者应用程序的家目录,依赖于平台。

 

再来看一下如何取得Caches目录路径的方法:

 NSString *s = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];

返回结果:

/Users/zhanggui/Library/Developer/CoreSimulator/Devices/B6134687-CC10-40DE-BD9D-A2E5AD2D5C03/data/Containers/Data/Application/DB647E93-5F05-4B7B-A8E9-F6F01DD2586B/Library/Caches

最后看一下如何获取tmp目录:

 NSString *s = NSTemporaryDirectory();

返回结果:

/Users/zhanggui/Library/Developer/CoreSimulator/Devices/B6134687-CC10-40DE-BD9D-A2E5AD2D5C03/data/Containers/Data/Application/C7CA833E-8D20-480B-AB91-234A8F17336D/tmp/

--------------------------------------------------------------------大概就这么多吧--------------------------------------------------------------------

NO ,NO ,NO看看苹果官方的说明吧:

AppName.app

This is the app’s bundle. This directory contains the app and all of its resources.

You cannot write to this directory. To prevent tampering, the bundle directory is signed at installation time. Writing to this directory changes the signature and prevents your app from launching. You can, however, gain read-only access to any resources stored in the apps bundle. For more information, see theResource Programming Guide

The contents of this directory are not backed up by iTunes. However, iTunes does perform an initial sync of any apps purchased from the App Store.

Documents/

Use this directory to store user-generated content. The contents of this directory can be made available to the user through file sharing; therefore, his directory should only contain files that you may wish to expose to the user.

The contents of this directory are backed up by iTunes.

Documents/Inbox

Use this directory to access files that your app was asked to open by outside entities. Specifically, the Mail program places email attachments associated with your app in this directory. Document interaction controllers may also place files in it.

Your app can read and delete files in this directory but cannot create new files or write to existing files. If the user tries to edit a file in this directory, your app must silently move it out of the directory before making any changes.

The contents of this directory are backed up by iTunes.

Library/

This is the top-level directory for any files that are not user data files. You typically put files in one of several standard subdirectories. iOS apps commonly use the Application Support and Caches subdirectories; however, you can create custom subdirectories.

Use the Library subdirectories for any files you don’t want exposed to the user. Your app should not use these directories for user data files.

The contents of the Library directory (with the exception of the Caches subdirectory) are backed up by iTunes.

For additional information about the Library directory and its commonly used subdirectories, see The Library Directory Stores App-Specific Files.

tmp/

Use this directory to write temporary files that do not need to persist between launches of your app. Your app should remove files from this directory when they are no longer needed; however, the system may purge this directory when your app is not running.

The contents of this directory are not backed up by iTunes.

英语好的大神可以看一下。不好的飘过,,,,

更多请见:https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html