Post

Replies

Boosts

Views

Activity

Reply to How to obtain photo data/metadata after being picked in PHPickerViewController?
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
Aug ’20
Reply to Drag & Drop with NSTableViewDiffableDataSource
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
Jul ’21