Class

NSSharingService

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

Declaration

@interface NSSharingService : NSObject

Overview

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

Topics

Creating a Sharing Service

+ sharingServiceNamed:

Returns a sharing service instance representing the specified service name.

- initWithTitle:image:alternateImage:handler:

Creates a custom sharing service object.

NSSharingServiceName

These constants list the available sharing services.

Sharing Items and Customizing the UI

delegate

Specifies the delegate of the sharing service.

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

menuItemTitle

The title of the service in the Share menu.

title

The title of the sharing service.

accountName

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

Available Sharing Services

+ sharingServicesForItems:

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

Sharing Items

- canPerformWithItems:

Returns whether the service can share all the specified items.

- performWithItems:

Manually performs the service on the provided items.

recipients

An array containing the user handles of the desired recipients.

Setting Content Attributes

alternateImage

The alternate image representing the sharing service.

image

The primary image representing the sharing service.

subject

The subject of the post.

Obtaining Information About a Completed Post

attachmentFileURLs

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

messageBody

The message body as a string.

permanentLink

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

Relationships

Inherits From

See Also

App Services

NSSharingServicePicker

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

NSSharingServicePickerDelegate

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

NSServicesMenuRequestor

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