NSItemProvider and Folders

It's possible to receive a folder of content dropped onto an app in iOS 11 (perhaps from the Files app) by using something like this in the drop handler:


  [itemProvider loadDataRepresentationForTypeIdentifier:(__bridge NSString *)kUTTypeFolder
  completionHandler:^(NSData * _Nullable data, NSError * _Nullable error)
  {
     // data contains a zip of the folder's files and subfolders.
  }];


But what is the correct way to set up an item provider in an app so that when the content is dropped in the other direction into the Files app, it appears as a folder containing files/subfolders?


Trying something like this doesn't work:


  itemProvider = [[[NSItemProvider alloc] init] autorelease];

  [itemProvider registerDataRepresentationForTypeIdentifier:(__bridge NSString *)kUTTypeFolder
    visibility:NSItemProviderRepresentationVisibilityAll
    loadHandler:^NSProgress* _Nullable(void (^ _Nonnull completionHandler)(NSData*  _Nullable, NSError * _Nullable))
  {
    NSProgress *progress = ...;

    NSData* data = [NSData dataWithContentsOfFile:pathToZippedFolderAndContents];

    completionHandler(data, nil);


    return progress;
  }];
Answering three years later for posterity -

Rather than providing the zipped contents, provide a URL to the folder itself. Here's the basic idea:

Code Block swift
let folderUrl = ...
let itemProvider = NSItemProvider()
itemProvider.suggestedName = folderUrl.lastPathComponent
itemProvider.registerFileRepresentation(forTypeIdentifier: "public.folder", visibility: .all) {
$0(folderUrl, false, nil) // wild stab in the dark for mysterious undocumented boolean
return nil
}


NSItemProvider and Folders
 
 
Q