iOS 沙盒探究

说这个之前,我们先了解一个工具,以便我们更好的去操作。

woodpecker.png

在appstore上搜索下载,以便我们更好的查看app的沙盒文件。同时在项目中导入它:pod 'WoodPeckeriOS',这样项目运行的时候就可以直观的看到app的沙盒文件。

当然不用这种软件也可以查看运行app的沙盒文件。
Xcode -> Window -> Devices and Simulators ->选择你要查看的应用 -> Download Container...
下载即可。


然后查看下载内容,显示包内容,就可以看到:

一、iOS沙盒机制简介

iOS中的沙盒机制(SandBox)是一种安全体系,它规定了应用程序只能在为该应用创建的文件夹内读取文件,不可以访问其他地方的内容。所有的非代码文件都保存在这个地方,比如图片、声音、属性列表和文本文件等。总体来说沙盒就是一种独立、安全、封闭的空间。

1.特点:

  • 每个应用程序的活动范围都限定在自己的沙盒里。
  • 不能随意跨越自己的沙盒去访问别的应用程序(iOS 8已经部分开放访问extension。
  • 在访问别人沙盒内的数据时需要访问权限。

2.沙盒目录

  • Documnets目录: 保存应用运行时生成的需要持久化的数据,iTunes备份和恢复的时候会包括此目录,所以苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下。
  • Library/Caches: 存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出后删除 。一般存放体积比较大,不是特别重要的资源。
  • Library/Preferences: 保存应用程序的所有偏好设置iOS的Settings(设置),我们不应该直接在这里创建文件,而是需要通过UserDefault这个类来访问应用程序的偏好设置。iTunes会自动备份该文件目录下的内容。
  • SystemData目录:暂无信息。
  • tmp目录:用于存放临时文件,保存应用程序再次启动过程中不需要的信息,重启后清空。

二、获取文件路径

常用获取文件路径方法主要有两种:

  1. NSHomeDirectory() + "/../../"
  2. NSSearchPathForDirectoriesInDomains方法

1. Documnets

let documnetPaths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documnetPath = documnetPaths.first

or

let documnetPath = NSHomeDirectory() + "/Documents"

2.Library/Caches

let cachePaths = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)
let cachePath = cachePaths.first

or

let cachePath = NSHomeDirectory() + "/Library/Caches

3.Library/Preferences

let preferencPath = NSHomeDirectory() + "/Library/Preferences

4.tmp

let timDir = NSTemporaryDirectory()

or

let timDir = NSHomeDirectory() + "/tmp"

三、文件常规操作

iOS开发经常会遇到读文件,写文件等,对文件和文件夹的操作,这时就可以使用FileManager,FileHandle等类来实现。
这个时候结合woodpecker效果会更好

1.创建文件

方法1:

let myDirectory = NSHomeDirectory() + "/Documents/myFolder"
let fileManager = FileManager.default
// 为ture表示路径中间如果有不存在的文件夹都会创建
try! fileManager.createDirectory(atPath: myDirectory, withIntermediateDirectories: true, attributes: nil)

方法2

let manager = FileManager.default
let urlForDocument = manager.urls(for: .documentDirectory, in: .userDomainMask)
let url = urlForDocument.first!
let folder = url.appendingPathComponent("myFolder", isDirectory: true)
let exist = manager.fileExists(atPath: folder.path)
if !exist {
    try! manager.createDirectory(at: folder, withIntermediateDirectories: true, attributes: nil)
}

结果可以看出,我们在Documents目录下成功创建一个myFolder文件夹:(↓:是创建出来的意思 ,↑:是删除的意思)


2.将对象写入文件

这里的对象包含String,NSString,UIImage,NSArray,NSDictionary,通过write(to:)方法即可。

2.1 string

let filePath = NSHomeDirectory() + "/Documents/wh.txt"
let info = "test write 'hello word' to wh.txt"
try? info.write(toFile: filePath, atomically: true, encoding: String.Encoding.utf8)

2.2 image

let filePath = NSHomeDirectory() + "/Documents/wh.png"
let image = UIImage.init(named: "WechatIMG9320")
let data: Data = UIImagePNGRepresentation(image!)!
try? data.write(to: URL.init(fileURLWithPath: filePath))

2.3 NSArray

let array = NSArray.init(array: ["a", "b", "c", "d"])
let filePath = NSHomeDirectory() + "/Documents/array.plist"
array.write(toFile: filePath, atomically: true)

2.4 NSDictionary

let dictionary = NSDictionary.init(dictionary: ["name": "wh",
                                                "age": 16,
                                                "sex": "man"])
let filePath = NSHomeDirectory() + "/Documents/Dic.plist"
dictionary.write(toFile: filePath, atomically: true)

3.创建文件

let manager = FileManager.default
let urlForDocument = manager.urls(for: .documentDirectory, in: .userDomainMask)
let url = urlForDocument.first!
let file = url.appendingPathComponent("test.txt")
let exist = manager.fileExists(atPath: file.path)
if !exist {
    let data = Data.init(base64Encoded: "aGVsbG8gd29ybGQ=", options: .ignoreUnknownCharacters)
    let creatSuccess = manager.createFile(atPath: file.path, contents: data, attributes: nil)
    print("文件创建结果:\(creatSuccess)")
}

屏幕快照 2018-11-22 下午4.28.20.png

4.复制文件

let fileManager = FileManager.default
let sourcePath = NSHomeDirectory() + "/Documents/wh.txt"
let toPath = NSHomeDirectory() + "/Documents/copy.txt"
try? fileManager.copyItem(atPath: sourcePath, toPath: toPath)

or

let manager = FileManager.default
let urlForDocument = manager.urls(for: .documentDirectory, in: .userDomainMask)
let url = urlForDocument.first!
let sourceURL = url.appendingPathComponent("wh.txt")
let toUrl = url.appendingPathComponent("copy.txt")
try? manager.copyItem(at: sourceURL, to: toUrl)

5.移动文件

let fileManager = FileManager.default
let sourceUrl = NSHomeDirectory() + "/Documents/test.txt"
let toUrl = NSHomeDirectory() + "/Documents/myFolder/test.txt"
try? fileManager.moveItem(atPath: sourceUrl, toPath: toUrl)

or

let fileManager = FileManager.default
let urlForDocument = fileManager.urls(for: .documentDirectory, in: .userDomainMask)
let url = urlForDocument.first!

let sourceUrl = url.appendingPathComponent("wh.txt")
let toUrl = url.appendingPathComponent("/myFolder/move.txt")
try! fileManager.moveItem(at: sourceUrl, to: toUrl)

6.删除目录下所有文件

let fileManager = FileManager.default
let myDirectory = NSHomeDirectory() + "/Documents/myFolder"
let fileArray = fileManager.subpaths(atPath: myDirectory)
for fn in fileArray!{
    try! fileManager.removeItem(atPath: myDirectory + "/\(fn)")
}

7.遍历一个目录下的所有文件

let manager = FileManager.default
let urlForDocument = manager.urls(for: .documentDirectory, in: .userDomainMask)
let url = urlForDocument.first
// 对指定路径执行浅搜索,返回指定目录路径下的文件、子目录及符号链接的列表
let contentsOfPath = try? manager.contentsOfDirectory(atPath: url!.path)
print("contentsOfPath:\(contentsOfPath!)")

let contentsOfUrl = try? manager.contentsOfDirectory(at: url!, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
print("contentsOfUrl:\(contentsOfUrl!)")

// 深度遍历,会递归遍历子文件夹(但不会递归符号链接)
let enumeratorAtPath = manager.enumerator(atPath: url!.path)
print("enumeratorAtPath:\(enumeratorAtPath!.allObjects)")

let enumeratorAtUrl = manager.enumerator(at: url!, includingPropertiesForKeys: nil, options: .skipsHiddenFiles, errorHandler: nil)
print("enumeratorAtUrl:\(enumeratorAtUrl!.allObjects)")

// 深度遍历,会递归遍历子文件夹(包括符号链接,所以要求性能的话用enumeratorAtPath)
let subPaths = manager.subpaths(atPath: url!.path)
print("subPaths:\(subPaths!)")

运行结果:

contentsOfPath:["wh.png", "Dic.plist", "myFolder", "array.plist", "copy.txt"]
contentsOfUrl:[file:///private/var/mobile/Containers/Data/Application/C0142560-B8D9-4620-A1A2-ACFBE26296AE/Documents/wh.png, file:///private/var/mobile/Containers/Data/Application/C0142560-B8D9-4620-A1A2-ACFBE26296AE/Documents/Dic.plist, file:///private/var/mobile/Containers/Data/Application/C0142560-B8D9-4620-A1A2-ACFBE26296AE/Documents/myFolder/, file:///private/var/mobile/Containers/Data/Application/C0142560-B8D9-4620-A1A2-ACFBE26296AE/Documents/array.plist, file:///private/var/mobile/Containers/Data/Application/C0142560-B8D9-4620-A1A2-ACFBE26296AE/Documents/copy.txt]
enumeratorAtPath:[wh.png, Dic.plist, myFolder, array.plist, copy.txt]
enumeratorAtUrl:[file:///private/var/mobile/Containers/Data/Application/C0142560-B8D9-4620-A1A2-ACFBE26296AE/Documents/wh.png, file:///private/var/mobile/Containers/Data/Application/C0142560-B8D9-4620-A1A2-ACFBE26296AE/Documents/Dic.plist, file:///private/var/mobile/Containers/Data/Application/C0142560-B8D9-4620-A1A2-ACFBE26296AE/Documents/myFolder/, file:///private/var/mobile/Containers/Data/Application/C0142560-B8D9-4620-A1A2-ACFBE26296AE/Documents/array.plist, file:///private/var/mobile/Containers/Data/Application/C0142560-B8D9-4620-A1A2-ACFBE26296AE/Documents/copy.txt]
subPaths:["wh.png", "Dic.plist", "myFolder", "array.plist", "copy.txt"]

8.判断文件或文件夹是否存在

let fileManager = FileManager.default
let filePath = NSHomeDirectory() + "/Documents/Folder"
let exist = fileManager.fileExists(atPath: filePath)
print("exist:\(exist)")

运行结果:

exist:false

如果文件夹名称是是myFolder,运行结果即为:exist:true

9.读取文件内容

let fileManager = FileManager.default
let urlForDocument = fileManager.urls(for: .documentDirectory, in: .userDomainMask)
let docPath = urlForDocument.first!
let file = docPath.appendingPathComponent("copy.txt")

//方法1
let readHandler = try! FileHandle.init(forReadingFrom: file)
let data = readHandler.readDataToEndOfFile()
let string = String.init(data: data, encoding: .utf8)
print("文件内容:\(string!)")

//方法2
let data2 = fileManager.contents(atPath: file.path)
let string2 = String.init(data: data2!, encoding: .utf8)
print("文件内容:\(string2!)")

运行结果:

文件内容:test write 'hello word' to wh.txt

10.写入数据

let fileManager = FileManager.default
let urlForDocument = fileManager.urls(for: .documentDirectory, in: .userDomainMask)
let docPath = urlForDocument.first!
let file = docPath.appendingPathComponent("copy.txt")

let string = "ending..."
let data = string.data(using: .utf8, allowLossyConversion: true)
let handler = try? FileHandle.init(forWritingTo: file)
// handler?.seek(toFileOffset: <#T##UInt64#>)
handler?.seekToEndOfFile()
handler?.write(data!)

11.文件属性

let fileManager = FileManager.default
let urlForDocument = fileManager.urls(for: .documentDirectory, in: .userDomainMask)
let docPath = urlForDocument.first!
let file = docPath.appendingPathComponent("copy.txt")

let attributes = try? fileManager.attributesOfItem(atPath: file.path)
//        print("attributes:\(attributes)")
print("创建时间:\(attributes![FileAttributeKey.creationDate]!)")
print("修改时间:\(attributes![FileAttributeKey.modificationDate]!)")
print("文件大小:\(attributes![FileAttributeKey.size]!)")

运行结果:

创建时时\351\227间:2018-11-23 03:22:35 +0000
修改\346\227时\346\227时\351\346\227时\351\351\227间:2018-11-23 04:02:12 +0000
文件大小:42

12.文件权限

let fileManager = FileManager.default
let urlForDocument = fileManager.urls(for: .documentDirectory, in: .userDomainMask)
let docPath = urlForDocument.first!
let file = docPath.appendingPathComponent("copy.txt")

let readable = fileManager.isReadableFile(atPath: file.path)
let writeable = fileManager.isWritableFile(atPath: file.path)
let executable = fileManager.isExecutableFile(atPath: file.path)
let deleteable = fileManager.isDeletableFile(atPath: file.path)
print("可读:\(readable) \n可写:\(writeable) \n可执行:\(executable) \n可删除:\(deleteable)")

运行结果:

可读:true 
可写:true 
可执行:false 
可删除:true

13.文件比较

let fileManager = FileManager.default
let urlForDocument = fileManager.urls(for: .documentDirectory, in: .userDomainMask)
let docPath = urlForDocument.first!
let contents = try! fileManager.contentsOfDirectory(atPath: docPath.path)
//下面比较用户文档中前面两个文件是否内容相同(该方法也可以用来比较目录)
let count = contents.count
if count > 1 {
    let path1 = docPath.path + "/" + contents[0]
    let path2 = docPath.path + "/" + contents[1]
    let equal = fileManager.contentsEqual(atPath: path1, andPath: path2)
    print("path1:\(path1)")
    print("path2:\(path2)")
    print("比较结果:\(equal)")
}

运行结果:

path1:/var/mobile/Containers/Data/Application/A1F0DBBF-1BDC-4527-957A-B205A580926B/Documents/wh.png
path2:/var/mobile/Containers/Data/Application/A1F0DBBF-1BDC-4527-957A-B205A580926B/Documents/Dic.plist
比较结果:false

以上就是沙盒文件的一些常规操作。

你可能感兴趣的:(iOS 沙盒探究)