How to show sharing button in Mountain Lion?

Mountain Lion offers a built-in sharing button that shows a menu of sharing services suitable for the application:

Share button in Safari 6.0

How can I embed it in my application?

+7
source share
3 answers

To add a sharing button to Mountain Lion:

1) Add an NSButton , e.g. shareButton .

2) Add a standard image for this button:

 [shareButton setImage:[NSImage imageNamed:NSImageNameShareTemplate]]; [shareButton sendActionOn:NSLeftMouseDownMask]; 

3) In the "on click" action, enter NSSharingServicePicker :

 NSSharingServicePicker *sharingServicePicker = [[NSSharingServicePicker alloc] initWithItems:urls]; sharingServicePicker.delegate = self; [sharingServicePicker showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMinYEdge]; 

4) Finally, apply the NSSharingServicePickerDelegate methods to configure the available selection tools.

+21
source

Please note that if you are trying to add this button through Interface Builder:

  • Select button
  • Switch to attribute inspector
  • Delete Title Button
  • Insert: NSShareTemplate as the image name.

In Xcode, this does not look good, but it works great at startup.

PS - This is apparently the case when you need to use the string value of the System Icon (NSShareTemplate) instead of the constant (NSImageNameShareTemplate).

+1
source

In Swift, I used this:

 extension NSSharingService { class func shareContent ( content: [AnyObject], button: NSButton ) { let sharingServicePicker = NSSharingServicePicker (items: content ) sharingServicePicker.showRelativeToRect(button.bounds, ofView: button, preferredEdge: NSRectEdge.MaxY) } } 
0
source

All Articles