Hi,
My Mac app allows a customer to drag and drop a file package onto a SwiftUI view. I can't seem to find a way to successfully call .startAccessingSecurityScopedResource() with the file/dir that was dropped into the view.
I put together a simple test app. Here is the code:
struct ContentView: View {
@State var isTargetedForDrop: Bool = false
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
Rectangle()
.stroke(Color.gray)
.onDrop(of: [UTType.fileURL], isTargeted: $isTargetedForDrop) { providers in
guard let provider = providers.first(where: { $0.hasItemConformingToTypeIdentifier(UTType.fileURL.identifier) }) else {
return false
}
provider.loadItem(forTypeIdentifier: UTType.fileURL.identifier, options: nil) { item, error in
if let error = error {
print("Drop load error: \(error)")
return
}
if let url = item as? URL {
print("Dropped file URL: \(url)")
} else if let data = item as? Data,
let url = URL(dataRepresentation: data, relativeTo: nil) {
print("Dropped file URL (from data): \(url)")
let access = url.startAccessingSecurityScopedResource()
if access {
print("Successfully accessed file at URL: \(url)")
} else {
print("Failed to access file at URL: \(url)")
}
url.stopAccessingSecurityScopedResource()
} else {
print("Unsupported dropped item: \(String(describing: item))")
}
}
return true
}
}
.padding()
}
}
When I drop a file package into this view I see, "Failed to access file at URL: <the_full_file_path>"
I'm running Xcode 26 on macOS 26.