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
Post
Replies
Boosts
Views
Activity
It turns out that you need to subclass NSTableViewDiffableDataSource and implement the drag and drop API in that subclass. It's kinda weird, but that is what needs to be done.
In my case, what I did was this:
implement all my drag and drop API that I usually would in my MyViewController
subclass NSTableViewDiffableDataSource to MyDiffableDataSource
added delegate APIs to MyDiffableDataSource that would ask the delegate for the drag and drop code. For example, it would do something like this:
in the MyDiffableDataSource.h file I would have this:
@class MyDiffableDataSource;
@protocol MyDiffableDataSourceDelegate <NSObject>
- (nullable id <NSPasteboardWriting>)tableView:(NSTableView *)tableView
pasteboardWriterForRow:(NSInteger)row;
- (void)tableView: (NSTableView *)tableView
draggingSession: (NSDraggingSession *)session
willBeginAtPoint: (NSPoint)screenPoint
forRowIndexes: (NSIndexSet *)rowIndexes;
- (NSDragOperation)tableView: (NSTableView *)tableView
validateDrop: (id <NSDraggingInfo>)draggingInfo
proposedRow: (NSInteger)row
proposedDropOperation: (NSTableViewDropOperation)dropOperation;
- (BOOL)tableView: (NSTableView *)tableView
acceptDrop: (id <NSDraggingInfo>)draggingInfo
row: (NSInteger)row
dropOperation: (NSTableViewDropOperation)dropOperation;
- (void)tableView: (NSTableView *)tableView
draggingSession: (NSDraggingSession *)session
endedAtPoint: (NSPoint)screenPoint
operation: (NSDragOperation)operation;
@end
@interface MyDiffableDataSource : NSTableViewDiffableDataSource
@property (weak, nonatomic, readwrite) id <MyDiffableDataSourceDelegate> delegate;
@end
In the MyDiffableDataSource.m file, it would do something like this:
- (nullable id <NSPasteboardWriting>)tableView:(NSTableView *)tableView
pasteboardWriterForRow:(NSInteger)row
{
if ([self.delegate respondsToSelector: @selector(tableView:pasteboardWriterForRow:)]){
return [self.delegate tableView: tableView
pasteboardWriterForRow: row];
}
return nil;
}
.
.
.
etc
I did not want to have drag and drop code in different classes, which I why I implemented step 2 and 3 using delegate messaging. Of course, I would need MyViewController to be set as the delegate of MyDiffableDataSource.
Too bad there is no real documentation that easily goes over this. You have to guess it when reading that NSTableViewDIffableDataSource conforms to NSTableViewDataSource.
EH
Have you figured out how to overcome the single item limitation on macOS?
I'm on macOS 14.5 and it still seems to have the single-item-drag bug.
Test Application here (I'm not certain how to make a public link on iCloud, so reply if it does not work):
https://www.icloud.com/iclouddrive/0867sf1ayePj8vd2gZblEL0UA#TableStressorTestMacOS
I'll try again:
https://www.icloud.com/iclouddrive/08ci-kngZMd8_d30RGUhPs__w#TableStressorTestMacOS
Sorry, iCloud public sharing is not very obvious.
in any case, the Developer Tech Support is:
Case-ID: 9474931
Maybe this Dropbox link is better:
https://www.dropbox.com/scl/fi/i9usljzvqvae8lifxv3yj/TableStressorTestMacOS.zip?rlkey=agupzzdm9wr8v96lbuh9mm3mg&st=r4qtvjvl&dl=0