for me, adding the video as bookmarkData (after granting full access to photos)
broken code:
.fileImporter(
isPresented: $isShowingFileImporter,
allowedContentTypes: [.movie, .mpeg4Movie],
allowsMultipleSelection: false
) { result in
do {
guard let selected = try result.get().first else { return }
// ✅ BEGIN security-scoped access
let _ = selected.startAccessingSecurityScopedResource()
defer { selected.stopAccessingSecurityScopedResource() }
selectedVideo = selected
print("✅ Selected from Files: \(selected)")
} catch {
print("❌ File selection error: \(error.localizedDescription)")
}
}
working code:
.fileImporter(
isPresented: $isShowingFileImporter,
allowedContentTypes: [.movie, .mpeg4Movie],
allowsMultipleSelection: false
) { result in
do {
guard let selected = try result.get().first else { return }
// Start security-scoped access
guard selected.startAccessingSecurityScopedResource() else {
print("❌ Failed to start accessing security-scoped resource")
return
}
// Create a bookmark for persistent access
let bookmarkData = try selected.bookmarkData(options: .minimalBookmark, includingResourceValuesForKeys: nil, relativeTo: nil)
UserDefaults.standard.set(bookmarkData, forKey: "selectedVideoBookmark")
selectedVideo = selected
print("✅ Selected from Files: \(selected)")
} catch {
print("❌ File selection error: \(error.localizedDescription)")
}
}