Mac Developer Library

Developer

AppKit Framework Reference NSSharingService Class Reference

Options
Deployment Target:

On This Page
Language:

NSSharingService

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];
  • }

Inheritance


Conforms To


Import Statement


Swift

import AppKit

Objective-C

@import AppKit;

Availability


Available in OS X v10.8 and later.
  • Returns a sharing service instance representing the specified service name.

    Declaration

    Swift

    init?(named serviceName: String) -> NSSharingService

    Objective-C

    + (NSSharingService *)sharingServiceNamed:(NSString *)serviceName

    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.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.8 and later.

  • Creates a custom sharing service object.

    Declaration

    Swift

    init(title title: String, image image: NSImage, alternateImage alternateImage: NSImage?, handler block: () -> Void)

    Objective-C

    - (instancetype)initWithTitle:(NSString *)title image:(NSImage *)image alternateImage:(NSImage *)alternateImage handler:(void (^)(void))block

    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.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.8 and later.

  • title title Property

    The title of the sharing service. (read-only)

    Declaration

    Swift

    var title: String? { get }

    Objective-C

    @property(readonly, copy) NSString *title

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.8 and later.

  • The title of the service in the Share menu.

    Declaration

    Swift

    var menuItemTitle: String?

    Objective-C

    @property(copy) NSString *menuItemTitle

    Discussion

    By default, this title is the same as the value of the title property. Your app can modify this value.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.9 and later.

  • The account name used for posting on Twitter or Sina Weibo. (read-only)

    Declaration

    Swift

    var accountName: String? { get }

    Objective-C

    @property(readonly, copy) NSString *accountName

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.9 and later.

  • Returns a list of sharing services which could share all the provided items together.

    Declaration

    Swift

    class func sharingServicesForItems(_ items: [AnyObject]) -> [AnyObject]?

    Objective-C

    + (NSArray *)sharingServicesForItems:(NSArray *)items

    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.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.8 and later.

  • Returns whether the service can share all the specified items.

    Declaration

    Swift

    func canPerformWithItems(_ items: [AnyObject]?) -> Bool

    Objective-C

    - (BOOL)canPerformWithItems:(NSArray *)items

    Parameters

    items

    The items to share.

    Return Value

    YEStrue if the service can share all the items; NOfalse otherwise. If items is nil, the method will return YEStrue 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.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.8 and later.

  • Manually performs the service on the provided items.

    Declaration

    Swift

    func performWithItems(_ items: [AnyObject])

    Objective-C

    - (void)performWithItems:(NSArray *)items

    Parameters

    items

    The items to share.

    Discussion

    In most cases this will display a sharing window.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.8 and later.

  • An array containing the user handles of the desired recipients.

    Declaration

    Swift

    var recipients: [AnyObject]?

    Objective-C

    @property(copy) NSArray *recipients

    Discussion

    Each object in the array is an NSString object that contains the handle of a single recipient. The specific format of these handle varies from service to service. For example, some services use email addresses as handles.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.9 and later.

  • The alternate image representing the sharing service. (read-only)

    Declaration

    Swift

    var alternateImage: NSImage? { get }

    Objective-C

    @property(readonly, strong) NSImage *alternateImage

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.8 and later.

    See Also

    title
    image

  • image image Property

    The primary image representing the sharing service. (read-only)

    Declaration

    Swift

    var image: NSImage { get }

    Objective-C

    @property(readonly, strong) NSImage *image

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.8 and later.

  • subject subject Property

    The subject of the post.

    Declaration

    Swift

    var subject: String?

    Objective-C

    @property(copy) NSString *subject

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.9 and later.

  • An array of NSURL objects representing the files that were shared. (read-only)

    Declaration

    Swift

    var attachmentFileURLs: [AnyObject]? { get }

    Objective-C

    @property(readonly, copy) NSArray *attachmentFileURLs

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.9 and later.

  • The message body as a string. (read-only)

    Declaration

    Swift

    var messageBody: String? { get }

    Objective-C

    @property(readonly, copy) NSString *messageBody

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.9 and later.

  • A permanent URL (permalink) that your app can use to access the post. (read-only)

    Declaration

    Swift

    @NSCopying var permanentLink: NSURL? { get }

    Objective-C

    @property(readonly, copy) NSURL *permanentLink

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.9 and later.

  • These constants list the available sharing services.

    Declaration

    Swift

    let NSSharingServiceNameAddToAperture: NSString! let NSSharingServiceNameAddToIPhoto: NSString! let NSSharingServiceNameAddToSafariReadingList: NSString! let NSSharingServiceNameComposeEmail: NSString! let NSSharingServiceNameComposeMessage: NSString! let NSSharingServiceNamePostImageOnFlickr: NSString! let NSSharingServiceNamePostOnFacebook: NSString! let NSSharingServiceNamePostOnLinkedIn: NSString! let NSSharingServiceNamePostOnSinaWeibo: NSString! let NSSharingServiceNamePostOnTencentWeibo: NSString! let NSSharingServiceNamePostOnTwitter: NSString! let NSSharingServiceNamePostVideoOnTudou: NSString! let NSSharingServiceNamePostVideoOnVimeo: NSString! let NSSharingServiceNamePostVideoOnYouku: NSString! let NSSharingServiceNameSendViaAirDrop: NSString! let NSSharingServiceNameUseAsDesktopPicture: NSString! let NSSharingServiceNameUseAsFacebookProfileImage: NSString! let NSSharingServiceNameUseAsLinkedInProfileImage: NSString! let NSSharingServiceNameUseAsTwitterProfileImage: NSString!

    Objective-C

    NSString * const NSSharingServiceNameAddToAperture; NSString * const NSSharingServiceNameAddToIPhoto; NSString * const NSSharingServiceNameAddToSafariReadingList; NSString * const NSSharingServiceNameComposeEmail; NSString * const NSSharingServiceNameComposeMessage; NSString * const NSSharingServiceNamePostImageOnFlickr; NSString * const NSSharingServiceNamePostOnFacebook; NSString * const NSSharingServiceNamePostOnLinkedIn; NSString * const NSSharingServiceNamePostOnSinaWeibo; NSString * const NSSharingServiceNamePostOnTencentWeibo; NSString * const NSSharingServiceNamePostOnTwitter; NSString * const NSSharingServiceNamePostVideoOnTudou; NSString * const NSSharingServiceNamePostVideoOnVimeo; NSString * const NSSharingServiceNamePostVideoOnYouku; NSString * const NSSharingServiceNameSendViaAirDrop; NSString * const NSSharingServiceNameUseAsDesktopPicture; NSString * const NSSharingServiceNameUseAsFacebookProfileImage; NSString * const NSSharingServiceNameUseAsLinkedInProfileImage; NSString * const NSSharingServiceNameUseAsTwitterProfileImage;

    Constants

    • NSSharingServiceNameAddToAperture

      NSSharingServiceNameAddToAperture

      Adds the content to Aperture.

      Available in OS X v10.8 and later.

    • NSSharingServiceNameAddToIPhoto

      NSSharingServiceNameAddToIPhoto

      Adds the content to iPhoto.

      Available in OS X v10.8 and later.

    • NSSharingServiceNameAddToSafariReadingList

      NSSharingServiceNameAddToSafariReadingList

      Add the content to the Safari Reading List.

      Available in OS X v10.8 and later.

    • NSSharingServiceNameComposeEmail

      NSSharingServiceNameComposeEmail

      Creates an email messages with the content.

      Available in OS X v10.8 and later.

    • NSSharingServiceNameComposeMessage

      NSSharingServiceNameComposeMessage

      Creates a Messages methods with the content.

      Available in OS X v10.8 and later.

    • NSSharingServiceNamePostImageOnFlickr

      NSSharingServiceNamePostImageOnFlickr

      Posts the image on Flickr.

      Available in OS X v10.8 and later.

    • NSSharingServiceNamePostOnFacebook

      NSSharingServiceNamePostOnFacebook

      Posts the content on Facebook.

      Available in OS X v10.8 and later.

    • NSSharingServiceNamePostOnLinkedIn

      NSSharingServiceNamePostOnLinkedIn

      Posts the content on LinkedIn.

      Available in OS X v10.9 and later.

    • NSSharingServiceNamePostOnSinaWeibo

      NSSharingServiceNamePostOnSinaWeibo

      Posts 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.

    • NSSharingServiceNamePostOnTencentWeibo

      NSSharingServiceNamePostOnTencentWeibo

      Posts the content on a Tencent Weibo, Chinese microblogging (weibo) website.

      Available in OS X v10.9 and later.

    • NSSharingServiceNamePostOnTwitter

      NSSharingServiceNamePostOnTwitter

      Posts the content on Twitter.

      Available in OS X v10.8 and later.

    • NSSharingServiceNamePostVideoOnTudou

      NSSharingServiceNamePostVideoOnTudou

      Posts the video on the video sharing service Tudou, based in the People's Republic of China.

      Available in OS X v10.8 and later.

    • NSSharingServiceNamePostVideoOnVimeo

      NSSharingServiceNamePostVideoOnVimeo

      Posts the video on the video sharing service Vimeo.

      Available in OS X v10.8 and later.

    • NSSharingServiceNamePostVideoOnYouku

      NSSharingServiceNamePostVideoOnYouku

      Posts the video on the video sharing service Youku, based in the People's Republic of China.

      Available in OS X v10.8 and later.

    • NSSharingServiceNameSendViaAirDrop

      NSSharingServiceNameSendViaAirDrop

      Sends the file via Air Drop.

      Available in OS X v10.8 and later.

    • NSSharingServiceNameUseAsDesktopPicture

      NSSharingServiceNameUseAsDesktopPicture

      Replaces the user’s desktop image with the content.

      Available in OS X v10.8 and later.

    • NSSharingServiceNameUseAsFacebookProfileImage

      NSSharingServiceNameUseAsFacebookProfileImage

      Replaces the user’s Facebook profile image with the provided image.

      Available in OS X v10.9 and later.

    • NSSharingServiceNameUseAsLinkedInProfileImage

      NSSharingServiceNameUseAsLinkedInProfileImage

      Replaces the user’s LinkedIn profile image with the provided image.

      Available in OS X v10.9 and later.

    • NSSharingServiceNameUseAsTwitterProfileImage

      NSSharingServiceNameUseAsTwitterProfileImage

      Replaces the user’s Twitter profile image with the provided image.

      Available in OS X v10.8 and later.