Created
September 26, 2012 10:24
-
-
Save aquarius/3787222 to your computer and use it in GitHub Desktop.
Use NSSharingService from a text only NSToolbar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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