NSSharingService Class Reference
| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/AppKit.framework |
| Availability | Available in OS X v10.8 and later. |
| Declared in | NSSharingService.h |
Overview
The NSSharingService class is used to provide a consistent user experience when sharing items on OS X. For example, items can be: NSURL objects, NSString objects, NSImage objects, video (through file URLs), or any object which implements the NSPasteboardWriting protocol.
For any item or group of items, the NSSharingService will display a sharing sheet to allow the user to pre-visualize what will be shared to the service. A sharing service can: create a post on a social network like Twitter or Facebook, send a message by email or iMessage, upload videos to viewing services, or send a file by AirDrop.
It's possible for developers to use NSSharingService directly. For instance, they can create a button that will trigger a service as shown in this code snippet:
- (void)awakeFromNib |
{ |
NSSharingService * service = [NSSharingService sharingServiceNamed:NSSharingServiceNamePostOnTwitter]; |
[myShareOnTwitterButton setTitle:service.title]; |
[myShareOnTwitterButton setEnabled:[service canPerformWithItems:nil]]; |
} |
- (IBAction)shareOnTwitter:(id)sender |
{ |
// Items to share |
NSAttributedString *text = [self.textView attributedString]; |
NSImage *image = [self.imageView image]; |
NSArray * shareItems = [NSArray arrayWithObjects:text, image, nil]; |
NSSharingService *service = [NSSharingService sharingServiceNamed:NSSharingServiceNamePostOnTwitter]; |
service.delegate = self; |
[service performWithItems:shareItems]; |
} |
Tasks
Creating a Sharing Service
Available Sharing Services
Sharing Items
Setting Content Attributes
-
alternateImageproperty -
imageproperty -
titleproperty
Getting and Setting the Delegate
-
delegateproperty
Properties
alternateImage
The alternate image representing the sharing service. (read-only)
Availability
- Available in OS X v10.8 and later.
See Also
Declared In
NSSharingService.hdelegate
Specifies the delegate of the sharing service.
Discussion
The delegate class must conform to the NSSharingServiceDelegate protocol.
Availability
- Available in OS X v10.8 and later.
Declared In
NSSharingService.himage
The primary image representing the sharing service. (read-only)
Availability
- Available in OS X v10.8 and later.
See Also
Declared In
NSSharingService.htitle
The title of the sharing service. (read-only)
Availability
- Available in OS X v10.8 and later.
See Also
Declared In
NSSharingService.hClass Methods
sharingServiceNamed:
Returns a sharing service instance representing the specified service name.
Parameters
- serviceName
The service name. The possible system provided values are listed in “Available Sharing Services.”
Return Value
An instance of NSSharingService for the specified service name.
Availability
- Available in OS X v10.8 and later.
Declared In
NSSharingService.hsharingServicesForItems:
Returns a list of sharing services which could share all the provided items together.
Parameters
- items
The items to share.
Return Value
An array of sharing services to allow for items.
Discussion
This method can be used to build a custom user interface or to populate a contextual menu.
Availability
- Available in OS X v10.8 and later.
Declared In
NSSharingService.hInstance Methods
canPerformWithItems:
Returns whether the service can share all the specified items..
Parameters
- items
The items to share.
Return Value
YES if the service can share all the items; NO otherwise. If items is nil, the method will return YES when the service is configured.
Discussion
This method can be used to validate a custom user interface such as a dedicated Twitter button. Therefore you could call it once at launch time with nil items to check whether to display the button or not, and then with real items to enable and disable the button depending on the context or selection.
Availability
- Available in OS X v10.8 and later.
See Also
Declared In
NSSharingService.hinitWithTitle:image:alternateImage:handler:
Creates a custom sharing service object.
Parameters
- title
The custom sharing service name.
- image
The image that represents the sharing service
- alternateImage
The alternate image that represents the sharing service
- block
The block that actually interacts with the service.
Return Value
An instance of the custom sharing object.
Discussion
Custom sharing services can be added to the NSSharingServicePicker with the sharingServicePicker:sharingServicesForItems:proposedSharingServices: delegate method.
When implementing this method, consider subclassing NSSharingService so the canPerformWithItems: and sharingServicesForItems: can provide accurate results.
Availability
- Available in OS X v10.8 and later.
Declared In
NSSharingService.hperformWithItems:
Manually performs the service on the provided items.
Parameters
- items
The items to share.
Discussion
In most cases this will display a sharing window.
Availability
- Available in OS X v10.8 and later.
See Also
Declared In
NSSharingService.hConstants
Available Sharing Services
These constants list the available sharing services.
NSString * const NSSharingServiceNamePostOnTwitter; NSString * const NSSharingServiceNamePostOnSinaWeibo; NSString * const NSSharingServiceNameComposeEmail; NSString * const NSSharingServiceNameComposeMessage; NSString * const NSSharingServiceNameSendViaAirDrop; NSString * const NSSharingServiceNameAddToSafariReadingList; NSString * const NSSharingServiceNameAddToIPhoto; NSString * const NSSharingServiceNameAddToAperture; NSString * const NSSharingServiceNamePostOnFacebook; NSString * const NSSharingServiceNameUseAsTwitterProfileImage; NSString * const NSSharingServiceNameUseAsDesktopPicture; NSString * const NSSharingServiceNamePostImageOnFlickr; NSString * const NSSharingServiceNamePostVideoOnVimeo; NSString * const NSSharingServiceNamePostVideoOnYouku; NSString * const NSSharingServiceNamePostVideoOnTudou;
Constants
NSSharingServiceNamePostOnTwitterPosts the content on Twitter.
Available in OS X v10.8 and later.
Declared in
NSSharingService.h.NSSharingServiceNamePostOnSinaWeiboPosts the content on a Sina Weibo, Chinese microblogging (weibo) website. Akin to a hybrid of Twitter and Facebook.
Available in OS X v10.8 and later.
Declared in
NSSharingService.h.NSSharingServiceNameComposeEmailCreates an email messages with the content.
Available in OS X v10.8 and later.
Declared in
NSSharingService.h.NSSharingServiceNameComposeMessageCreates a Messages methods with the content.
Available in OS X v10.8 and later.
Declared in
NSSharingService.h.NSSharingServiceNameSendViaAirDropSends the file via Air Drop.
Available in OS X v10.8 and later.
Declared in
NSSharingService.h.NSSharingServiceNameAddToSafariReadingListAdd the content to the Safari Reading List.
Available in OS X v10.8 and later.
Declared in
NSSharingService.h.NSSharingServiceNameAddToIPhotoAdds the content to iPhoto.
Available in OS X v10.8 and later.
Declared in
NSSharingService.h.NSSharingServiceNameAddToApertureAdds the content to Aperture.
Available in OS X v10.8 and later.
Declared in
NSSharingService.h.NSSharingServiceNameUseAsTwitterProfileImageReplaces the Twitter profile image with the content.
Available in OS X v10.8 and later.
Declared in
NSSharingService.h.NSSharingServiceNameUseAsDesktopPictureReplaces the user’s desktop image with the content.
Available in OS X v10.8 and later.
Declared in
NSSharingService.h.NSSharingServiceNamePostOnFacebookPosts the content on Facebook.
Available in OS X v10.8 and later.
Declared in
NSSharingService.h.NSSharingServiceNamePostImageOnFlickrPosts the image on Flickr.
Available in OS X v10.8 and later.
Declared in
NSSharingService.h.NSSharingServiceNamePostVideoOnVimeoPosts the video on the video sharing service Vimeo.
Available in OS X v10.8 and later.
Declared in
NSSharingService.h.NSSharingServiceNamePostVideoOnYoukuPosts the video on the video sharing service Youku, based in the People's Republic of China.
Available in OS X v10.8 and later.
Declared in
NSSharingService.h.NSSharingServiceNamePostVideoOnTudouPosts the video on the video sharing service Tudou, based in the People's Republic of China.
Available in OS X v10.8 and later.
Declared in
NSSharingService.h.
© 2012 Apple Inc. All Rights Reserved. (Last updated: 2012-09-19)