0%

Sandbox Browser

一个沙盒浏览小工具

在平常开发调试的过程中,经常会需要查看日志文件,我们一般会将日志文件放在 Documents 文件夹下,那么有哪些方式可以查看呢:

  1. xcode 工具,Windows -> Devices 进入设备管理页面,可以查看 iPhone 上所有 Installed Apps,再通过 Download Container 将 APP 信息下载到 Mac 上,就可以查看你想要的文件。
  2. 通过浏览器打开 APP 的沙盒文件,内置一个 HTTP 服务器,浏览器直接访问 sandbox 目录,这种方式还算方便。
  3. 在 iOS 端集成一段代码,通过摇一摇或者其他方式呼出一个文件浏览工具页面,选中需要的文件,点击直接查看或者通过 Airdrop 分享至 Mac 端查看。

相比于前两种方式,第三种方案实现简单,体验更顺畅,所以花时间实现做了一个小工具。

核心方法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/// About FileManager
extension WESandboxVC {
/// 加载指定路径下文件
fileprivate func loadPath(filePath: String) {
DispatchQueue.global().async {
var files: [WEFileItem] = []

var targetPath = filePath
if filePath.count == 0 || filePath == self.rootPath {
/// 进入主文件夹路径
targetPath = self.rootPath
}else {
/// 已经进入主文件夹内部,添加一个返回上一层的item
let item = WEFileItem(fileName: "🔙..", fileType: .backup, filePath: filePath)
files.append(item)
}

var paths: [String] = []
/// 取出路径下所有子路径
do {
paths = try FileManager.default.contentsOfDirectory(atPath: targetPath)
} catch {
print("no files exist!!!")
}

for inlinePath in paths {
/// 拼出完整路径
let fullPath = NSString(string: targetPath).appendingPathComponent(inlinePath)
var isDir: ObjCBool = false
/// 检查路径是否存在,且是否是文件夹
if !FileManager.default.fileExists(atPath: fullPath, isDirectory: &isDir) {
continue
}

var item = WEFileItem(fileName: "", fileType: .backup, filePath: fullPath)
if isDir.boolValue {
/// 文件夹
item.fileName = "📁 " + inlinePath
item.fileType = .dir
}else{
/// 文件
item.fileName = "📃 " + inlinePath
item.fileType = .file
}

files.append(item)
}
self.fileDatas = files
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}

/// 调用原生分享
fileprivate func sharePath(path: String) {
let url = URL(fileURLWithPath: path)
let objectNeedShare = [url]

let controller = UIActivityViewController(activityItems: objectNeedShare, applicationActivities: nil)
if UIDevice.current.model.contains("iPad") {
controller.popoverPresentationController?.sourceView = self.view
controller.popoverPresentationController?.sourceRect = CGRect(x: screenWidth * 0.5, y: screenHeight, width: 20, height: 20)
}
self.present(controller, animated: true, completion: nil)
}
}

只需要添加以下代码,可以通过摇一摇的方式呼出文件浏览器:

1
2
3
4
5
6
override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
super.motionEnded(motion, with: event)
#if DEBUG
WESandboxTool.switchSandbox()
#endif
}

Github地址