Class

NSSharingService

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

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

init?(named: NSSharingService.Name)

Returns a sharing service instance representing the specified service name.

var title: String

The title of the sharing service.

var menuItemTitle: String

The title of the service in the Share menu.

var accountName: String?

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

Available Sharing Services

class func sharingServices(forItems: [Any])

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

Sharing Items

func canPerform(withItems: [Any]?)

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.

Getting and Setting the Delegate

var delegate: NSSharingServiceDelegate?

Specifies the delegate of the sharing service.

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.

Constants

Available Sharing Services

These constants list the available sharing services.

Structures

struct NSSharingService.CloudKitOptions

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

Enumerations

enum NSSharingService.SharingContentScope

The sharing scope constants specify the nature of the things you are sharing.

Relationships

Inherits From

Conforms To

See Also

App Services

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.

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.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software