Contents

NSSharingService

An object that facilitates the sharing of content with social media services, or with apps like Mail or Safari.

Declaration

class NSSharingService

Overview

An NSSharingService object provides a consistent user experience for sharing items—NSURL objects, NSString objects, NSImage objects, video (through file URLs), of any object that implements the NSPasteboardWriting protocol—in macOS.

For any item or group of items, the NSSharingService displays a sheet with the content to share. 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 using AirDrop.

You can use NSSharingService objects directly in your app. The following example shows how to create a button that shares content directly to a social media service.

- (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

Managing the Delegate

Querying Service Availability

Getting the Service’s Details

Configuring the Service

Sharing Items

Providing CloudKit Share Options

Getting the Shared Items

See Also

App Services