gpt4 book ai didi

swift - 无法打开NSCocoaErrorDomain Code = 257文件,因为您无权查看: FileManager attributesOfItem returns nil in iOS13

转载 作者:行者123 更新时间:2023-12-04 12:50:21 29 4
gpt4 key购买 nike

在iOS 13设备中,尝试获取文件大小时,FileManager返回权限错误。

do {
let attr = try FileManager.default.attributesOfItem(atPath: my_file_path) //--> Getting nil
fileSize = attr[FileAttributeKey.size] as! UInt64
} catch {
print("Error: \(error)")
}

返回错误:

Error Domain=NSCocoaErrorDomain Code=257 "The file “trim.1A9FFC19-EE2C-438A-BF3D-97E05A97EF9E.MOV” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/private/var/mobile/Containers/Data/PluginKitPlugin/ADB8684E-12B5-451D-A20F-158B899BB3DD/tmp/trim.1A9FFC19-EE2C-438A-BF3D-97E05A97EF9E.MOV, NSUnderlyingError=0x280af0510 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}



仅当我更新到iOS 13之后,才观察到该问题。在早期版本中,一切正常。

最佳答案

iOS 13 SDK将照片应用程序视为另一个应用程序,因此当我们关闭图像选择器 Controller 时,视频网址将无效。

在尝试将视频上传到AWS之前,我遇到了问题,我只是创建了一个临时文件夹并在关闭Image-picker之前复制了现有的视频URL路径。然后将其上传,就可以了。

    func createDirectory(videoURL:URL){
let Directorypath = getDirectoryPath()
var objcBool:ObjCBool = true
let isExist = FileManager.default.fileExists(atPath:Directorypath,isDirectory: &objcBool)
// If the folder with the given path doesn't exist already, create it
if isExist == false{
do{
try FileManager.default.createDirectory(atPath: Directorypath, withIntermediateDirectories: true, attributes: nil)
}catch{

print("Something went wrong while creating a new folder")
}
}
let fileManager = FileManager()

do {
if fileManager.fileExists(atPath:Directorypath) {
try? fileManager.removeItem(at: URL(fileURLWithPath:Directorypath))
}
try fileManager.copyItem(at:videoURL.absoluteURL, to: URL(fileURLWithPath:Directorypath))

self.imagePicker.dismiss(animated: true, completion:nil)
}catch let error {
print(error.localizedDescription)
}

}

关于swift - 无法打开NSCocoaErrorDomain Code = 257文件,因为您无权查看: FileManager attributesOfItem returns nil in iOS13,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58081267/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com