2

SwiftySandboxFileAccess在用户第一次批准 NSOpenPanel 选择时工作,但是下次打开应用程序时,即使 SwiftySandboxFileAccess 认为它​​具有访问权限,只是因为UserDefaults我认为保存了一个值,安全书签也没有访问权限?

这就是SwiftySandboxFileAccess节省UserDefaults

"bd_file:///Volumes/": <626f6f6b fc010000 00000410 30000000 75703e99 3136c5a3 905214f4 1d55afdf
 f9680acc 65a5c354 86b39a29 ee2e1683 1c010000 04000000 03030000 00080028 07000000 01010000
 566f6c75 6d657300 04000000 01060000 10000000 08000000 04030000 93d14501 00000000 04000000 
01060000 2c000000 08000000 00040000 41c188f9 52800000 18000000 01020000 02000000 00000000 
0f000000 00000000 00000000 00000000 08000000 01090000 66696c65 3a2f2f2f 0c000000 01010000 
4d616369 6e746f73 68204844 08000000 04030000 0050065e 3a000000 08000000 00040000 41c1ab00 
f8865976 24000000 01010000 35384630 35383433 2d414237 332d3442 34342d42 3344372d 34374434 
39373543 46434536 18000000 01020000 81000000 01000000 ef130000 01000000 00000000 00000000 
01000000 01010000 2f000000 00000000 01050000 a8000000 feffffff 01000000 00000000 0d000000 
04100000 20000000 00000000 05100000 3c000000 00000000 10100000 58000000 00000000 40100000 
48000000 00000000 02200000 08010000 00000000 05200000 78000000 00000000 10200000 88000000 
00000000 11200000 bc000000 00000000 12200000 9c000000 00000000 13200000 ac000000 00000000 
20200000 e8000000 00000000 30200000 14010000 00000000 10d00000 04000000 00000000>
  1. 它在路径bd_的开头添加了什么?file://
  2. 所有这些 8 位的东西是什么?

更多背景:

这就是我尝试使用安全书签的方式:

  func accessRunFileVacuum(args: VacuumCli) {
    let access = SandboxFileAccess()
    let success = access.access(

      fileURL: URL(fileURLWithPath: "/Volumes"),
      askIfNecessary: true,
      persistPermission: true, with: {
      
      print("access the URL here")
      self.runFileVacuumCLI(args)

    })

    print(" pickFile success: \(success)")
  }

"access the URL here"...,网址...嗯...

来自: https ://github.com/ConfusedVorlon/SwiftySandboxFileAccess/blob/master/SwiftySandboxFileAccessDemo/SwiftySandboxDemo/Manager.swift#L42

我正在阅读相关的安全书签票证,其中提到安全书签需要保存为 URL,然后传递到要使用它的子进程: https ://stackoverflow.com/a/27321020/1762493

with: {}块已通过此处看到的 SwiftySandboxFileAccess包装在startAccessingSecurityScopedResource/中: https ://github.com/ConfusedVorlon/SwiftySandboxFileAccess/blob/master/SwiftySandboxFileAccess/Classes/SandboxFileAccess.swift#L87stopAccessingSecurityScopedResource

runFileVacuumCLI()作为队列操作运行:

  func runFileVacuumCLI(_ args: VacuumCli) {
    checkVolumesAccess()

    if queue.operationCount < 1 {

      fvOperation = FileVacuumOperation(vacuumCli: args)
      queue.addOperations([fvOperation], waitUntilFinished: false)
      
    }
  }
4

1 回答 1

0

SandboxFileAccess().requestPermissions()startAccessingSecurityScopedResource调用时允许控制。securityScopedFileURL?.startAccessingSecurityScopedResource() == true结果在尝试访问文件之前以某种方式调用一次就可以了。

要清楚,SandboxFileAccess().access()也调用startAccessingSecurityScopedResource(),然后执行一个传递block{},然后调用stopAccessingSecurityScopedResource()。由于我的函数是异步的,stop因此被调用并且我对该文件的访问权限已关闭。

let success = access.requestPermissions(
    forFileURL: URL(fileURLWithPath: bodyString),
    askIfNecessary: true,
    persistPermission: true, with: { securityScopedFileURL, bookmarkData in

      if (securityScopedFileURL?.startAccessingSecurityScopedResource() == true) {
        self.testLog(" gainAccess inside startAccessingSecurityScopedResource() == true")
      }
})

self.testLog(" pickFile success: \(success)")

我相信这与startAccessingSecurityScopedResource另一个答案中提到的“平衡”有关: https ://stackoverflow.com/a/24301326/1762493

于 2020-09-16T02:08:06.827 回答