SwiftySandboxFileAccess works the first time the user approves the NSOpenPanel selection, but the next time the app opens, the security bookmark doesn't have access even though SwiftySandboxFileAccess thinks it has access just because a value is saved in the UserDefaults
I think?
This is what SwiftySandboxFileAccess
is saving in 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>
bd_
it added at the beginning of the file://
path?More Context:
This is how I'm trying to use the security bookmark:
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"
..., URL... hum... 🤔
I was reading a related security bookmarks ticket and it mentioned that the Security Bookmark needs to be saved as a URL and then passed into the sub-process that wants to use it: https://stackoverflow.com/a/27321020/1762493
the with: {}
block is wrapped in startAccessingSecurityScopedResource
/stopAccessingSecurityScopedResource
already via SwiftySandboxFileAccess seen here: https://github.com/ConfusedVorlon/SwiftySandboxFileAccess/blob/master/SwiftySandboxFileAccess/Classes/SandboxFileAccess.swift#L87
runFileVacuumCLI()
runs as a Queue Operation:
func runFileVacuumCLI(_ args: VacuumCli) {
checkVolumesAccess()
if queue.operationCount < 1 {
fvOperation = FileVacuumOperation(vacuumCli: args)
queue.addOperations([fvOperation], waitUntilFinished: false)
}
}
SandboxFileAccess().requestPermissions()
allowed control when startAccessingSecurityScopedResource
was invoked. Turns out somehow calling securityScopedFileURL?.startAccessingSecurityScopedResource() == true
once before trying to access the file did the trick.
To be clear, SandboxFileAccess().access()
also calls startAccessingSecurityScopedResource()
, then executes a passed block{}
, then calls stopAccessingSecurityScopedResource()
. Since my function is async, stop
was getting called and my access was closed on that file.
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)")
I believe this is related to the "balancing of startAccessingSecurityScopedResource
" mentioned in this other answer:
https://stackoverflow.com/a/24301326/1762493