UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: UIDocumentPickerViewController & .stopAccessingSecurityScopedResource()

Forums > iOS

I have a viewcontroller that presents my custom ImagePicker class. One of the presented options is to select an image from 'Files', using UIDocumentPickerViewController. Picking an image works fine, but i want close the security resources conform the recommendations.

ProjectimagePicker {
            (...)
            let documentsPicker = UIDocumentPickerViewController(documentTypes: ["public.image", "public.jpeg", "public.png"], in: .open)
            documentsPicker.delegate = self
            documentsPicker.allowsMultipleSelection = false
            documentsPicker.modalPresentationStyle = .fullScreen
            self.presentationController?.present(documentsPicker, animated: true, completion: nil)
}

//MARK: - Ext. Delegate DocumentPicker
extension ProjectImagePicker: UIDocumentPickerDelegate {
    public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        guard controller.documentPickerMode == .open, let url = urls.first, url.startAccessingSecurityScopedResource() else { return }
        defer {
            DispatchQueue.main.async {
                url.stopAccessingSecurityScopedResource()
            }
             }

        guard let image = UIImage(contentsOfFile: url.path) else { return }
        self.delegate?.didSelect(image: image)
        controller.dismiss(animated: true)
    }

    public func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
        controller.dismiss(animated: true)
    }
}

And in the viewController that called the picker class:

//MARK: - Delegate ProjectImagePicker
extension ProjectDetailsViewController: ProjectImagePickerDelegate {
    func didSelect(image: UIImage?) {
        if let image  = image {
            selectedImage = image
            projectImageView.image = image
        }
    }
}

Part of the problem I circumvent by wrapping a dispatch call around stopAccessingSecurityScopedResource(). The image gets send back (delegate) and presented in the viewController. But when I eventually save (write to documents directory) my project and that image (selectedImage), I get the security error

2020-05-08 13:54:04.429936+0200 ProjectS[3482:1339708] [ProjectS] createDataWithMappedFile:1524:  'open' failed '/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/10.JPEG'  error = 1 (Operation not permitted)

So, apperently the image is still referencing the URL from the Documents picker. I could try and save the image temporarily in the didPickDocumentsAt method, but that seems ugly. Or I can omit calling stopAccessingSecurityScopedResource(0 all together, but that may cause problems? Any ideas on how to hanlde this the best possible way ?

3      

If I update my code for didPickDocumentsAt in the way that I make a copy of the image, all works as intended

//MARK: - Ext. Delegate DocumentPicker
extension ProjectImagePicker: UIDocumentPickerDelegate {
    public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        guard controller.documentPickerMode == .open, let url = urls.first, url.startAccessingSecurityScopedResource() else { return }
        defer {
            DispatchQueue.main.async {
                url.stopAccessingSecurityScopedResource()
            }
             }

        //Need to make a new image with the jpeg data to be able to close the security resources!
        guard let image = UIImage(contentsOfFile: url.path), let imageCopy = UIImage(data: image.jpegData(compressionQuality: 1.0)!) else { return }

        self.delegate?.didSelect(image: imageCopy)
        controller.dismiss(animated: true)
    }

Good solution, or better ideas?

4      

I think this makes sense that you get the image and then save the data instead of holding reference to stuff from iCloud Drive

4      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.