Gizmodo
Gizmodo

Reputation: 3222

PHAssetResourceManager writeData Returning Error 1

I am trying to share a PHAssetResource with the original file name. This method saves the asset resource to temp directory and shares from there. Since iOS 13, this has been giving an error:

let tempDirectory = NSTemporaryDirectory() as NSString
let partialPath = tempDirectory.appendingPathComponent(sentAssetResource.originalFilename)
let fileURL = URL(fileURLWithPath: partialPath)

let options = PHAssetResourceRequestOptions()
options.isNetworkAccessAllowed = true


PHAssetResourceManager.default().writeData(for: sentAssetResource, toFile: fileURL, options: options, completionHandler: { (error) in

    if (error != nil) {

        print (error?.localizedDescription)
        return
    }

    DispatchQueue.main.async {
        let activityViewController = UIActivityViewController(activityItems: [fileURL],  applicationActivities: nil)

        activityViewController.completionWithItemsHandler = {
            (activity, success, items, error) in

            if FileManager.default.fileExists(atPath: fileURL.path) {
                try? FileManager.default.removeItem(atPath: fileURL.path)
            }
        }

        self.present(activityViewController, animated: true, completion: {


        })
    }
})

The error received is:

The operation couldn't be completed.
(PHPhotosErrorDomain error -1.)

What could be wrong here?

I had to switch to PHAssetResourceManager.default().requestData, but this shares without the proper file name.

Upvotes: 1

Views: 844

Answers (1)

holtmann
holtmann

Reputation: 6293

The method works fine on iOS 13.2 in my testing. It's possible that the method fails for you cause of a database error (the iOS Photo Library is a CoreData database and AssetResources are a separate table). Can you reproduce the issue in the simulator as well as on all test devices?

Upvotes: 3

Related Questions