NSSharingService Sharing Submenu

久未见 提交于 2019-12-09 08:33:44

问题


How do I add the Share submenu in a Mac app? An example is Safari > File > Share. I poked at the Apple SharingServices sample code, but it does not include a working menu item.

Right now I have a button that displays a picker of available sharing services when tapped:

NSMutableArray *shareItems = [NSMutableArray arrayWithObject:[self.noteSynopsisView string]];
NSSharingServicePicker *sharingServicePicker = [[NSSharingServicePicker alloc] initWithItems:shareItems];
sharingServicePicker.delegate = self;
[sharingServicePicker showRelativeToRect:[self.shareButton bounds] ofView:self.shareButton preferredEdge:NSMaxYEdge];

I've also defined a Share submenu item under the File menu for my MainWindow.xib.

As I understand it, the NSSharingService list is being generated on the fly. So I can't really predefine the services to the menu item I have created in Interface Builder.

Thanks for your help.


回答1:


Look at NSSharingService's +sharingServicesForItems:. In a -validateMenuItem: method you could create a submenu using the -title and -image of the NSSharingServices it returns. Associate each service with each menu item, and point the action of each menu item at this:

- (IBAction)shareFromService:(id)sender {
  [[sender representedObject] performWithItems: arrayOfItemsToShare];
}

It's really quite simple. Apple did a good job on this one.




回答2:


I find the gist can help you easily create a submenu of proper services. https://gist.github.com/eternalstorms/4132533

It's a NSSharingServicePicker category.



来源:https://stackoverflow.com/questions/12378595/nssharingservice-sharing-submenu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!