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:

  1. - (void)awakeFromNib
  2. {
  3. NSSharingService * service = [NSSharingService sharingServiceNamed:NSSharingServiceNamePostOnTwitter];
  4. [myShareOnTwitterButton setTitle:service.title];
  5. [myShareOnTwitterButton setEnabled:[service canPerformWithItems:nil]];
  6. }
  7. - (IBAction)shareOnTwitter:(id)sender
  8. {
  9. // Items to share
  10. NSAttributedString *text = [self.textView attributedString];
  11. NSImage *image = [self.imageView image];
  12. NSArray * shareItems = [NSArray arrayWithObjects:text, image, nil];
  13. NSSharingService *service = [NSSharingService sharingServiceNamed:NSSharingServiceNamePostOnTwitter];
  14. service.delegate = self;
  15. [service performWithItems:shareItems];
  16. }
  • 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.

    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.

    Availability

    Available in OS X v10.8 and later.

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

    Declaration

    Swift

    var recipients: [String]?

    Objective-C

    @property(copy) NSArray <NSString *> *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.

    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

    Availability

    Available in OS X v10.8 and later.

    See Also

    title
    image

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

    Declaration

    Swift

    var image: NSImage { get }

    Objective-C

    @property(readonly, strong) NSImage *image

    Availability

    Available in OS X v10.8 and later.

  • The subject of the post.

    Declaration

    Swift

    var subject: String?

    Objective-C

    @property(copy) NSString *subject

    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: [NSURL]? { get }

    Objective-C

    @property(readonly, copy) NSArray <NSURL *> *attachmentFileURLs

    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

    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

    Availability

    Available in OS X v10.9 and later.

  • These constants list the available sharing services.

    Declaration

    Swift

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

    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.