捕获从磁盘读取文件时出错 - 您没有权限

问题描述

我在 AppDel 中使用以下代码。当用户点击 gpx 文件或使用共享选项与我的应用程序共享文件时触发。在这一点上,是用户指定他们允许我的应用程序访问该文件,所以我对为什么这仍然被拒绝感到有些困惑。非常感谢任何建议。

func application(_ app: UIApplication,open url: URL,options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool {
    
    if let dir = FileManager.default.urls(for: .documentDirectory,in: .userDomainMask).first {
        let fileURL = dir.appendingPathComponent(url.lastPathComponent)
        if FileManager.default.fileExists(atPath: fileURL.path) {
            print("File already exists")
        }
        else {
            do {
                try FileManager.default.copyItem(at: url,to: fileURL)
                print("Did write file to disk")
            }
             catch {
                 DispatchQueue.main.async(){
                         print("Catch error writing file to disk: \(error)")
                 }
             }
        }
    }
    return true
}

错误打印到控制台如下:

Error Domain=NSCocoaErrorDomain Code=257 "文件 “Beech_Hill_Long_Route.gpx”无法打开,因为您没有 允许查看它。” UserInfo={NSFilePath=/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/Desktop/Beech_Hill_Long_Route.gpx,NSUnderlyingError=0x282c19a70 {错误域=NSPOSIXErrorDomain 代码=1 “不允许操作”}}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)