[iOSAppOnMac] ShareKit: Crashes in SHKItemIsPDF() or -[SHKSaveToFilesSharingService saveFileURL:completion:]

I have a custom document-based iOS app that also runs on macOS. After implementing -activityItemsConfiguration to enable sharing from the context menu, I found that the app crashes on macOS when selecting Share… from the context menu and then selecting Save (i.e. Save to Files under iOS). This problem does not occur on iOS, which behaves correctly.

- (id<UIActivityItemsConfigurationReading>)activityItemsConfiguration {
    NSItemProvider * provider = [[NSItemProvider alloc] initWithContentsOfURL:self.document.presentedItemURL];
    UIActivityItemsConfiguration * configuration = [UIActivityItemsConfiguration activityItemsConfigurationWithItemProviders:@[ provider ]];

    // *** crashes with com.apple.share.System.SaveToFiles

    return configuration;
}

Additionally, I found that to even reach this crash, the workaround implemented in the NSItemProvider (FBxxx) category of the sample project is needed. Without this, the app will crash much earlier, due to SHKItemIsPDF() erroneously invoking -pathExtension on an NSItemProvider. This appears to be a second bug in Apple’s private ShareKit framework.

#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>

@implementation NSItemProvider (FBxxx)
// *** SHKItemIsPDF() invokes -pathExtension on an NSItemProvider (when running under macOS, anyway) -> crash
- (NSString *)pathExtension {
    return self.registeredContentTypes.firstObject.preferredFilenameExtension;
}
@end

Again, this all works fine on iOS (17.5) but crashes when the exact same app build is running on macOS (14.5).

I believe these bugs are Apple's. Any idea how to avoid the crash? Is there a way to disable the "Save to Files" option in the sharing popup?

I filed FB13819800 with a sample project that demonstrates the crash on macOS. I was going to file a TSI to get this resolved, but I see that DTS is not responding to tech support incidents until after WWDC.

[iOSAppOnMac] ShareKit: Crashes in SHKItemIsPDF() or -[SHKSaveToFilesSharingService saveFileURL:completion:]
 
 
Q