android 沙盒 ios,iOS开发关于沙盒

每个iOS程序都有一个独立的文件系统(存储空间),而且只能在对应的文件系统中进行操作,此区域被称为沙盒。应用必须待在自己的沙盒里,其他应用不能访问该沙盒。所有的非代码文件都要保存在此,例如属性文件plist、文本文件、图像、图标、媒体资源等。

d0376fb9ec71?utm_campaign=maleskine...&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

iOS app 沙盒目录结构

沙盒中相关路径

AppName.app 应用程序的程序包目录,包含应用程序的本身。由于应用程序必须经过签名,所以不能在运行时对这个目录中的内容进行修改,否则会导致应用程序无法启动。

Documents/ 保存应用程序的重要数据文件和用户数据文件等。用户数据基本上都放在这个位置(例如从网上下载的图片或音乐文件),该文件夹在应用程序更新时会自动备份,在连接iTunes时也可以自动同步备份其中的数据。

Library:这个目录下有两个子目录,可创建子文件夹。可以用来放置您希望被备份但不希望被用户看到的数据。该路径下的文件夹,除Caches以外,都会被iTunes备份.

Library/Caches: 保存应用程序使用时产生的支持文件和缓存文件(保存应用程序再次启动过程中需要的信息),还有日志文件最好也放在这个目录。iTunes 同步时不会备份该目录并且可能被其他工具清理掉其中的数据。

Library/Preferences: 保存应用程序的偏好设置文件。NSUserDefaults类创建的数据和plist文件都放在这里。会被iTunes备份。

tmp/: 保存应用运行时所需要的临时数据。不会被iTunes备份。iPhone重启时,会被清空。

// 获取沙盒根目录路径

NSString *homeDir = NSHomeDirectory();

// 获取Documents目录路径

NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];

//获取Library的目录路径

NSString *libDir = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES) lastObject];

// 获取cache目录路径

NSString *cachesDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES) firstObject];

// 获取tmp目录路径

NSString *tmpDir =NSTemporaryDirectory();

// 获取应用程序程序包中资源文件路径的方法:

NSString *bundle = [[NSBundle mainBundle] bundlePath];

NSLog(@"homeDir=%@ \n docDir=%@ \n libDir=%@ \n cachesDir=%@ \n tmpDir=%@ \n bundle=%@", homeDir,docDir, libDir, cachesDir, tmpDir, bundle);

结果为:

homeDir=/var/mobile/Containers/Data/Application/FBE41071-F5DD-4F02-9DFC-E9E473D3C917

docDir=/var/mobile/Containers/Data/Application/FBE41071-F5DD-4F02-9DFC-E9E473D3C917/Documents

libDir=/var/mobile/Containers/Data/Application/FBE41071-F5DD-4F02-9DFC-E9E473D3C917/Library

cachesDir=/var/mobile/Containers/Data/Application/FBE41071-F5DD-4F02-9DFC-E9E473D3C917/Library/Caches

tmpDir=/private/var/mobile/Containers/Data/Application/FBE41071-F5DD-4F02-9DFC-E9E473D3C917/tmp/

bundle=/var/containers/Bundle/Application/4A42EF08-30BA-4C19-B0C9-62903300C2BC/SandboxDemo.app

关于NSSearchPathForDirectoriesInDomains函数

FOUNDATION_EXPORT NSArray *NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory directory, NSSearchPathDomainMask domainMask, BOOL expandTilde); 用于查找目录,返回指定范围内的指定名称的目录的路径集合。有三个参数:

NSSearchPathDirectory directory 想要查找的目录,是个枚举值,有很多值,有关于iOS的,有关于macOS,也有关于watchOS的。

typedef NS_ENUM(NSUInteger, NSSearchPathDirectory) {

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

};

NSSearchPathDomainMask domainMask 表示“想要从哪个路径区域保护区查找”。

typedef NS_OPTIONS(NSUInteger, NSSearchPathDomainMask) {

NSUserDomainMask =1, // 用户的主目录

NSLocalDomainMask =2, // 当前机器的本地目录

NSNetworkDomainMask =4, //在网络中公开可用的位置

NSSystemDomainMask =8, // 被苹果系统提供的,不可更改的位置 (/System)

NSAllDomainsMask = 0x0ffff // 上述所有及未来的位置

};

BOOL expandTilde 表示是否用波浪线显示部分目录路径。~在*nix系统表示当前用户的Home目录。列如上面获取cache目录路径如果使用NO,那么结果就是cachesDir=~/Library/Caches

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS安卓沙盒是用来保护应用程序数据的安全性的一种机制。它们的主要区别如下: 1. 安全性:iOS沙盒机制相对安卓更加严格。iOS要求应用程序在自己的沙盒内运行,限制了应用之间的直接访问和交互。而安卓沙盒机制相对较弱,应用程序可以更容易地访问和共享数据。 2. 文件系统结构:iOS沙盒机制将每个应用程序的文件分隔为多个目录,包括应用程序包、文档目录、缓存目录等。每个目录只能由特定的应用程序进行访问和写入。而安卓沙盒机制通常是基于应用的用户ID,并且应用程序可以自由地向沙盒中的文件系统写入和访问。 3. 权限管理:iOS沙盒机制通过权限管理来限制应用程序对系统资源的访问。应用程序需要在安装时声明所需的访问权限,并由用户在使用时授予。而安卓沙盒机制在应用程序安装时会一次性获取所有权限,并且用户只能在软件设置中进行修改。 4. 应用程序的更新:iOS沙盒机制要求每个应用程序都在单独的容器中运行,因此应用程序的更新通常是通过替换整个应用程序包来完成的。而安卓沙盒机制允许应用程序在更新时只替换其中的部分文件,从而减少下载和更新时间。 综上所述,iOS安卓沙盒机制在安全性、文件系统结构、权限管理和应用程序更新方面存在一些差异。iOS沙盒机制相对更严格,保护了应用程序和用户数据的安全性,但也可能限制了应用程序之间的交互。而安卓沙盒机制相对较弱,允许应用程序更自由地访问和共享数据,但也可能增加了安全隐患。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值