Skip to content

Instantly share code, notes, and snippets.

@aquarius
Created September 26, 2012 10:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aquarius/3787222 to your computer and use it in GitHub Desktop.
Save aquarius/3787222 to your computer and use it in GitHub Desktop.
Use NSSharingService from a text only NSToolbar
// In toolbarWillAddItem: create a new NSMenu
// set the delegate and add the menu as part of a NSMenuItem as menuFormRepresentation
- (void)menuNeedsUpdate:(NSMenu *)menu
{
// clear out in case we can no longer provide this service
[menu removeAllItems];
NSURL *fileURL = [self.document fileURL];
if (!fileURL) return;
// rebuild the menu
NSArray *sharingService = [NSSharingService sharingServicesForItems:[NSArray arrayWithObject:fileURL]];
for (NSSharingService *currentService in sharingService) {
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:currentService.title action:@selector(selectedSharingServiceFromMenuItem:) keyEquivalent:@""];
item.image = currentService.image;
item.representedObject = currentService;
[menu addItem:item];
[item release];
}
}
- (void)selectedSharingServiceFromMenuItem:(NSMenuItem *)menuItem
{
NSURL *fileURL = [self.document fileURL];
if (!fileURL) return;
NSSharingService *service = menuItem.representedObject;
if (![service isKindOfClass:[NSSharingService class]]) return; // just to make sure…
service.delegate = self;
[service performWithItems:@[fileURL]];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment