For some strange reason, when I use 'file representation' loading, the app does not have the proper permissions to work on the file at the returned URL. I had to copy the file to a temp location and work on the copied URL. Not sure if this is intentional or an oversight bug. Below is the sort of code I needed to use:
NSString *allFiles = @"public.item";
_ _block PHPickerResult *blockResult = result;
_ _block NSMutableArray <NSURL *>*blockFileURLArray = [NSMutableArray array];
for (PHPickerResult *result in results){
[result.itemProvider loadFileRepresentationForTypeIdentifier: allFiles
completionHandler:^(NSURL * Nullable url,
NSError * Nullable error) {
if (error) {
// TODO
// show alert
NSLog(@"error %@", error);
} else {
// for some reason, I cannot access the url for uploading
// need to copy the file to a temp location
NSString *filename = url.lastPathComponent;
NSString *path = [NSString temporaryPathByUsingFileName: [filename stringByDeletingPathExtension]
pathExtension: filename.pathExtension];
NSURL *copyURL = [NSURL fileURLWithPath: path];
NSError *copyError;
[NSFileManager.defaultManager copyItemAtURL: url
toURL: copyURL
error: &copyError];
if (copyError) {
NSLog(@"%@ copyError %@", self, error);
} else {
// can now do stuff to the file at copyURL
// store it in the files array
[blockFileURLArray addObject: copyURL];
}
}
}
// do stuff on the gathered files in blockFileURLArray
Topic:
App & System Services
SubTopic:
Core OS
Tags: