当我测试 iOS 14 beta 时,此功能在部署在真实设备上时可以工作,但现在在生产中它只能在模拟器上工作。 iOS 14 有什么变化吗?我收到以下错误:
FilePickerTest[15486:5885508] [AXRuntimeCommon]未知客户端:FilePickerTest
文件“16010282771_20190702_23_38.csv”无法打开,因为您无权查看它。
我是否需要添加一项设置才能授予我的应用程序访问该文件的权限?
import SwiftUI
struct ContentView: View {
@State private var openFile = false
@StateObject var doc = ViewModel()
var body: some View {
VStack {
Text("Tap to pick a csv file")
.padding()
.onTapGesture(perform: {
openFile.toggle()
})
}
.fileImporter(isPresented: $openFile, allowedContentTypes: [.commaSeparatedText]) { (res) in
do {
let fileUrl = try res.get()
print(fileUrl)
doc.viewFile(fileUrl: fileUrl)
} catch {
print("Error reading file")
print(error.localizedDescription)
}
}
}
}
import SwiftUI
class ViewModel: ObservableObject {
var readString = ""
func viewFile(fileUrl: URL) {
do {
readString = try String(contentsOf: fileUrl)
} catch {
print("Error reading file")
print(error.localizedDescription)
}
print("File contents: \(readString)")
}
}
添加 fileUrl.startAccessingSecurityScopedResource()
前
doc.viewFile(文件地址: 文件地址)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)