Mac Catalyst 的 UIDocumentPickerViewController 配置
UIDocumentPickerViewController configuration for Mac Catalyst
将 UIDocumentPickerViewController
与 Mac Catalyst 一起使用时,是否需要额外的功能、权限或沙盒配置?我错过了警告吗?
运行 以下代码在带有 Catalyst 的 macOS (11.4) 上,选择文件时永远不会调用 didPickDocumentsAt:
委托函数。但是,如果关闭选择器或尝试打开文件,则会调用 documentPickerWasCancelled
。
我最初怀疑是由于 SwiftUI 的 View 结构重建导致的不正确的内容类型或委托解除分配,但是当调用 as cancel 时,我怀疑不是。
需要注意的是,在为 macOS 配置 UIDocumentPickerViewController
时,asCopy
必须为 false
,但为 iOS 配置 true
。
该错误表示沙盒权限错误:
DocumentPickerSwiftUI[42570:1472986] Failed to create an FPSandboxingURLWrapper for file:///Users/ed/test_file.txt. Error: Error Domain=NSPOSIXErrorDomain Code=1 "couldn't issue sandbox extension com.apple.app-sandbox.read-write for '/Users/ed/test_file.txt': Operation not permitted" UserInfo={NSDescription=couldn't issue sandbox extension com.apple.app-sandbox.read-write for '/Users/ed/test_file.txt': Operation not permitted}
当前能力配置(用户选择文件的只读权限):
import SwiftUI
import UIKit
import UniformTypeIdentifiers
struct ContentView: View {
@State var showDocumentPicker: Bool = false
var body: some View {
documentPickerButton
}
private var documentPickerButton: some View {
Button(action: {
showDocumentPicker.toggle()
}, label: {
Text("Open")
})
.sheet(isPresented: self.$showDocumentPicker) {
AudioFilePicker()
}
}
}
final class AudioFilePicker: UIViewControllerRepresentable {
var viewController = UIDocumentPickerViewController(forOpeningContentTypes: [.item], asCopy: false)
class Coordinator: NSObject, UIDocumentPickerDelegate {
var parent: AudioFilePicker
init(parent: AudioFilePicker) {
self.parent = parent
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
print("Picker Loaded: \(String(describing: urls.first?.absoluteString))")
// Do something with URL (never called)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
print("Picker Cancelled")
}
}
func makeCoordinator() -> Coordinator {
return Coordinator(parent: self)
}
func makeUIViewController(context: Context) -> UIDocumentPickerViewController {
viewController.delegate = context.coordinator
return viewController
}
func updateUIViewController(_ uiViewController: UIDocumentPickerViewController, context: Context) { }
}
该错误确实表明存在权限错误。尝试添加:
<key>com.apple.security.files.user-selected.read-write</key>
<true/>
将 UIDocumentPickerViewController
与 Mac Catalyst 一起使用时,是否需要额外的功能、权限或沙盒配置?我错过了警告吗?
运行 以下代码在带有 Catalyst 的 macOS (11.4) 上,选择文件时永远不会调用 didPickDocumentsAt:
委托函数。但是,如果关闭选择器或尝试打开文件,则会调用 documentPickerWasCancelled
。
我最初怀疑是由于 SwiftUI 的 View 结构重建导致的不正确的内容类型或委托解除分配,但是当调用 as cancel 时,我怀疑不是。
需要注意的是,在为 macOS 配置 UIDocumentPickerViewController
时,asCopy
必须为 false
,但为 iOS 配置 true
。
该错误表示沙盒权限错误:
DocumentPickerSwiftUI[42570:1472986] Failed to create an FPSandboxingURLWrapper for file:///Users/ed/test_file.txt. Error: Error Domain=NSPOSIXErrorDomain Code=1 "couldn't issue sandbox extension com.apple.app-sandbox.read-write for '/Users/ed/test_file.txt': Operation not permitted" UserInfo={NSDescription=couldn't issue sandbox extension com.apple.app-sandbox.read-write for '/Users/ed/test_file.txt': Operation not permitted}
当前能力配置(用户选择文件的只读权限):
import SwiftUI
import UIKit
import UniformTypeIdentifiers
struct ContentView: View {
@State var showDocumentPicker: Bool = false
var body: some View {
documentPickerButton
}
private var documentPickerButton: some View {
Button(action: {
showDocumentPicker.toggle()
}, label: {
Text("Open")
})
.sheet(isPresented: self.$showDocumentPicker) {
AudioFilePicker()
}
}
}
final class AudioFilePicker: UIViewControllerRepresentable {
var viewController = UIDocumentPickerViewController(forOpeningContentTypes: [.item], asCopy: false)
class Coordinator: NSObject, UIDocumentPickerDelegate {
var parent: AudioFilePicker
init(parent: AudioFilePicker) {
self.parent = parent
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
print("Picker Loaded: \(String(describing: urls.first?.absoluteString))")
// Do something with URL (never called)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
print("Picker Cancelled")
}
}
func makeCoordinator() -> Coordinator {
return Coordinator(parent: self)
}
func makeUIViewController(context: Context) -> UIDocumentPickerViewController {
viewController.delegate = context.coordinator
return viewController
}
func updateUIViewController(_ uiViewController: UIDocumentPickerViewController, context: Context) { }
}
该错误确实表明存在权限错误。尝试添加:
<key>com.apple.security.files.user-selected.read-write</key>
<true/>