Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing certain types of file (including zip) in Cocoa using NSSharingServiceNameComposeMessage

Tags:

cocoa

Here is my code:

          NSSharingService *service = [NSSharingService sharingServiceNamed: NSSharingServiceNameComposeMessage];
          tempURL = [NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"test.zip"]]
          [service performWithItems:@[@"test", tempURL]];

The message window is properly shown, as well as the 'test' text, but the file is not included in the message.

What works:

  • I know the URL is fine because it is correctly displayed when using the service named NSSharingServiceNameComposeEmail instead
  • the zip file is valid; it is a test file I am using already at a known location, and I already checked it can be properly expanded; it's also a very small file
  • I know a message can have a file attached: the above works for PDFs, Pages documents, etc...
  • I know my setup for Messages is fine and it can work with zip files: the 'share' button in Finder works fine when used with a zip file and selecting the 'Send Message' option

Similar issues:

  • unknown file types also don't work
  • same issue when using the Airdrop service NSSharingServiceNameSendViaAirDrop

Any ideas what else I could try? Thanks!

like image 438
charles Avatar asked Oct 31 '22 18:10

charles


1 Answers

Apparently you can share a zip file but It doesn't show up on the share sheet.

I tested It and when I sent the message to myself I got the zip file attached.

- (IBAction)shareZipFile:(id)sender {
    NSOpenPanel *openPanel = [NSOpenPanel openPanel];
    openPanel.allowedFileTypes = @[@"zip"];
    openPanel.prompt = @"Share";
    [openPanel runModal];

    NSSharingService *service = [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeMessage];
    NSArray *items = @[openPanel.URL.lastPathComponent.stringByDeletingPathExtension, openPanel.URL];

    if (![service canPerformWithItems:items]) {
        NSLog(@"Can't share that kind of stuff, sorry!");
        return;
    }

    [service performWithItems:items];
}
like image 108
Guilherme Rambo Avatar answered Jan 04 '23 15:01

Guilherme Rambo