Hi,
I'm working in a iOS app using the UIImagePickerController to get files and videos from the photo library and upload to Azure Blob storage.
For images, everything is working fine, but for videos, it fails with the following error:
Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" UserInfo={_kCFStreamErrorCodeKey=1, _kCFStreamErrorDomainKey=1}
Basically, what I'm doing is:
1.to get the file URL from the UIImagePickerController:
if mediaType == "public.movie" { let mediaURL = info[UIImagePickerController.InfoKey.mediaURL] as! NSURL uploadFileAsync(fileURL: mediaURL.filePathURL)
}
- the upload to Azure method:
func uploadFileAsync(fileURL: URL) { ...
do { let isSecureAccess = fileURL.startAccessingSecurityScopedResource()
defer{fileURL.stopAccessingSecurityScopedResource()}
let resourceValues = try fileURL.resourceValues(forKeys: [.fileSizeKey])
// Gets the Mime Type
let extUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileURL.pathExtension as CFString, nil)?.takeUnretainedValue()
let mimeType:String = extUTI != nil ? (UTTypeCopyPreferredTagWithClass(extUTI!, kUTTagClassMIMEType)?.takeUnretainedValue() as String?) ?? defaultMimeType : defaultMimeType
let fileSize: Int = resourceValues.fileSize!
blockBlob?.properties.length = NSNumber(value: fileSize)
blockBlob?.properties.contentType = mimeType
blockBlob?.uploadFromFile(with: fileURL)
}
The error is generating from the blockBlob?.uploadFromFile(with: fileURL)
Any help is really appreciated.
Unfortunately I can’t help you with a third-party library. If you have questions about that, you need to investigate the code yourself, assuming its open source, or seek support from the library vendor.
If this is library is using a standard session, you may be able to prevent this error by deferring your call to stopAccessingSecurityScopedResource() until after the upload is complete.
When you think this it makes sense: The URLSession code running in your process doesn’t know that the URL is security scoped and thus doesn’t call startAccessingSecurityScopedResource() / stopAccessingSecurityScopedResource() around its access. Rather, it assumes that it has access to the URL, which means that you have to make these calls on its behalf.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"