Class

NSSharingService

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

Overview

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

Symbols

Creating a Sharing Service

init?(named: String)

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.

Relationships

Inherits From

Conforms To