A service that lets users share content with other services, such as social media services, or apps, such as Mail and Safari.


class NSSharingService : NSObject


The NSSharingService class is used to provide a consistent user experience when sharing items—NSURL objects, NSString objects, NSImage objects, video (through file URLs), or any object that implements the NSPasteboardWriting protocol—in macOS.

For any item or group of items, the NSSharingService displays a sharing sheet that lets the user 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 example, 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];


Creating a Sharing Service

init?(named: NSSharingService.Name)

Returns a sharing service instance representing the specified service name.

struct NSSharingService.Name

These constants list the available sharing services.

Sharing Items and Customizing the UI

var delegate: NSSharingServiceDelegate?

Specifies the delegate of the sharing service.

protocol NSSharingServiceDelegate

A set of methods that you use to customize the position and animation of a share sheet, and to be notified whether the item is successfully shared.

Getting the Configuration

var menuItemTitle: String

The title of the service in the Share menu.

var title: String

The title of the sharing service.

var accountName: String?

The account name used for posting on Twitter or Sina Weibo.

Available Sharing Services

class func sharingServices(forItems: [Any]) -> [NSSharingService]

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

Sharing Items

func canPerform(withItems: [Any]?) -> Bool

Returns whether the service can share all the specified items.

func perform(withItems: [Any])

Manually performs the service on the provided items.

var recipients: [String]?

An array containing the user handles of the desired recipients.

Setting Content Attributes

var alternateImage: NSImage?

The alternate image representing the sharing service.

var image: NSImage

The primary image representing the sharing service.

var subject: String?

The subject of the post.

Obtaining Information About a Completed Post

var attachmentFileURLs: [URL]?

An array of NSURL objects representing the files that were shared.

var messageBody: String?

The message body as a string.

var permanentLink: URL?

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


struct NSSharingService.CloudKitOptions

Possible values returned by options(for:share:) that describe how the user is allowed to configure the related share.


Inherits From

Conforms To

See Also

App Services

class NSSharingServicePicker

A list of sharing services from which the user can choose.

protocol NSSharingServicePickerDelegate

A set of methods that you use to customize the service picker behavior.

protocol NSServicesMenuRequestor

A set of methods that support interaction with items users can share through a sharing service.

protocol NSCloudSharingServiceDelegate

A protocol that an iCloud-sharing service implements to handle status changes and errors.