urlswiftuiappstore-sandboxquicklooknsopenpanel

How to open file QuickLookPreview


I have code below to show file (PDF) preview via .quickLookPreview modifier

Button("Preview...") {
  previewURL = documentURL!
}
.disabled(documentURL == nil)
.quickLookPreview($previewURL)

It works when documentURL variable is assigned via NSOpenPanel but fails when I assign it from URL obtained from data storage.

Xcode complains:

Could not create FPSandboxingURLWrapper with url: file:///Users/...

How to allow opening file preview also for URLs obtained from storage?


Solution

  • When you save a URL to which you get access via a sandbox extension granted by the open panel, you need to save bookmark data to disk, which includes the sandbox extension (https://developer.apple.com/documentation/foundation/nsurl/1417795-bookmarkdata?cmdf=nsurl%20bookmarkdata) instead of saving the URL itself, which is just a glorified path without any access rules associated.