我希望能够将 pdf 文件从“文件”应用程序导入到我的应用程序,但不知道如何实现这一点。
当您向电子邮件添加附件时,是否有 FilePicker 或类似的东西可以用于与苹果邮件应用程序类似的行为?使用邮件应用程序,当您按“添加文档”时,它会打开一个文档浏览器,您可以在其中选择文件。我相信这是来自 iCloud Drive。
jn_*_*pdx 11
您可以.fileImporter在 SwiftUI 中使用:
struct ContentView : View {
@State private var presentImporter = false
var body: some View {
Button("Open") {
presentImporter = true
}.fileImporter(isPresented: $presentImporter, allowedContentTypes: [.pdf]) { result in
switch result {
case .success(let url):
print(url)
//use `url.startAccessingSecurityScopedResource()` if you are going to read the data
case .failure(let error):
print(error)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)