9
votes

FileManager returns permission error while trying to get the file size, in iOS 13 devices.

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

Error returned:

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"}}

The issue observed only after I updated to iOS 13. In earlier versions everything is working fine.

1
my s3 upload not working, throwing same issue. I was googling the issue for couple of days. tried url.startAccessingSecurityScopedResource() url.stopAccessingSecurityScopedResource(), no solution yetVijay

1 Answers

3
votes

iOS 13 SDK consider photo app as an another app, so when we dismiss the image picker controller video url will be invalidate.

I had the problem before when I try to upload video to AWS, what i did just create a temporary folder and copy the existing video url path before dismiss the Image-picker.then it upload, it's worked.

    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)
              }

   }