iOS之了解沙盒

矫情吗;* 2022-07-29 10:40 374阅读 0赞

1、Xcode

版本:7.2

语言:Swift2

2、沙盒路径

A、沙盒所在目录:

  1. print("沙盒所在目录:\(NSHomeDirectory())");

B、沙盒安装目录

  1. print("沙盒安装目录: \(NSBundle.mainBundle().bundlePath)");

观察运行结果。

  1. 沙盒所在目录:/Users/XXX/Library/Developer/CoreSimulator/Devices/3CA1FFF2-F868-4D3C-AA01-E4BEEEFBAC36/data/Containers/Data/Application/CAB24E30-8289-42B1-BD99-CC2E994E4540
  2. 沙盒安装目录: /Users/XXX/Library/Developer/CoreSimulator/Devices/3CA1FFF2-F868-4D3C-AA01-E4BEEEFBAC36/data/Containers/Bundle/Application/D58FD8F3-8223-4542-BEC6-CCA535D06011/沙盒.app

激活Finder,进入上面输出的路径中,会看到下图所示的目录结构图:

Center

Documents: 用户自己生产的数据保存在该目录下,iTunes备份和恢复的时候,会包括此目录。

Library:存储程序的默认设置,其他状态信息。

tmp:提供创建临时文件。

3、沙盒的操作

A、查询多沙盒的路径,源码如下:

  1. import UIKit
  2. class ViewController: UIViewController {
  3. override func viewDidLoad() {
  4. super.viewDidLoad()
  5. /// 查询
  6. querySandBoxOperation();
  7. }
  8. override func didReceiveMemoryWarning() {
  9. super.didReceiveMemoryWarning()
  10. }
  11. /// 自己添加的函数
  12. func querySandBoxOperation()
  13. {
  14. /// 获取home路径
  15. let homePath = NSHomeDirectory();
  16. print("沙盒的Home路径是:\(homePath)\n");
  17. /// 输出document路径
  18. let documentPath:Array = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as Array<String>;
  19. print("沙盒的document路径为:\(documentPath[0])\n");
  20. /// 输出library路径
  21. let libPath = NSSearchPathForDirectoriesInDomains(.LibraryDirectory, .UserDomainMask, true);
  22. print("沙盒的library路径为:\(libPath[0])\n");
  23. /// 输出tmp路径
  24. let tmpPath = NSTemporaryDirectory();
  25. print("沙盒的tmp路径为:\(tmpPath)\n");
  26. /// 输出沙盒的缓存目录
  27. let cachePath = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true);
  28. print("沙盒的缓存目录为:\(cachePath[0])");
  29. }
  30. }

B、读写文

document目录下,尝试读写文件,源码:

  1. /// 写操作
  2. func writeFileOperation()
  3. {
  4. let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as Array<String>;
  5. let documentPath:String? = path[0];
  6. if nil != documentPath
  7. {
  8. /// 文件路径
  9. let filePath = documentPath! + "ios.txt";
  10. /// 文件内容
  11. let fileContents:NSArray = ["hello", "swift"];
  12. let result = fileContents.writeToFile(filePath, atomically: true);
  13. result == true ? print("写入成功"):print("写入失败");
  14. }
  15. else
  16. {
  17. print("目录不存在");
  18. }
  19. }
  20. /// 读操作
  21. func readFileOperatiob()
  22. {
  23. let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as Array<String>;
  24. let documentPath:String? = path[0];
  25. let readPath = documentPath! + "ios.txt";
  26. let fileContent = NSArray(contentsOfFile: readPath);
  27. print("文件内容如下:\n\(fileContent!)");
  28. }

接着,调用写入和读写方法

  1. override func viewDidLoad() {
  2. super.viewDidLoad()
  3. /// 查询
  4. /// querySandBoxOperation();
  5. writeFileOperation();
  6. readFileOperatiob();
  7. }

运行结果如下:

Center 1

发表评论

表情:
评论列表 (有 0 条评论,374人围观)

还没有评论,来说两句吧...

相关阅读

    相关 查看iOS文件

    每一个iOS程序都一个自己的文件系统,这个文件系统叫应用程序沙盒(SanBox),它存放这代码以外的文件,其他的应用程序不能访问到该程序的沙盒, 如何在本地查看应用

    相关 iOS8路径问题

    在我们的印象中一个路径获取后,下一次再想进此路径只需原路径搜索即可。但在iOS8中,倘若再抱有此思想,我想只会碰壁。 1.在iOS8之前,我们获取到沙盒中的document、

    相关 iOS使用总结

    1沙盒机制 iPhone对于安装在上面的应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,这个区域就叫做沙盒。所有和该程序有关的文件都保存在这个独立

    相关 iOS系统的机制

    出于安全考虑, iOS将每个应用程序限制在属于自己的特定文件内,其他app没有权限访问到此文件夹, 这就是app的沙盒。 iOS的应用程序常用的文件目录:

    相关 iOS 机制

    iOS 每个 APP 都有自己的存储空间,这个存储空间叫做沙盒. APP可以在自己的沙盒中进行数据存取操作,但不能访问其他 app 的沙盒空间.对 app 做一些数据存储或者文