iOS Developer Library — Prerelease

Developer

Social Framework Reference SLComposeServiceViewController Class Reference

Options
Deployment Target:

On This Page
Language:

SLComposeServiceViewController

The SLComposeServiceViewController class provides a standard compose view you can present for social sharing extensions on both platforms. By default, the compose view includes items such as an editable text view and an indication of remaining characters, in addition to support for previewing attachments and displaying configuration items, such as an account or privacy picker.

The compose view controller gets items for the content and preview areas from the extensionContext property of the extension’s NSExtensionContext object.

  • Starts the animated dismissal of the compose view.

    Declaration

    Swift

    func cancel()

    Objective-C

    - (void)cancel

    Discussion

    When the cancel animation finishes, this method calls didSelectCancel. A subclass shouldn’t need to override cancel. In rare cases a subclass may call cancel, such as in response to a catastrophic failure during user interaction with the compose view.

    Availability

    Available in iOS 8.0 and later.

  • Sent to the compose view after the cancel animation finishes.

    Declaration

    Swift

    func didSelectCancel()

    Objective-C

    - (void)didSelectCancel

    Discussion

    By default, this method calls the completeRequestReturningItems: method of the associated extensionContext property, passing the appropriate error value in the items array and a nil expiration.

    Availability

    Available in iOS 8.0 and later.

  • Sent to the compose view after the post animation finishes.

    Declaration

    Swift

    func didSelectPost()

    Objective-C

    - (void)didSelectPost

    Discussion

    By default, this method calls the completeRequestReturningItems: method of the associated extensionContext property, passing nil in the items array and a nil expiration handler. You must override didSelectPost to perform the post of contentText and any attachments. In your implementation of this method, you can call super to take advantage of the default completion handler; if you don’t call super, you must call the completion method of the extension context.

    Availability

    Available in iOS 8.0 and later.

  • The number of characters remaining in a custom character limit.

    Declaration

    Swift

    var charactersRemaining: NSNumber!

    Objective-C

    @property(strong, nonatomic) NSNumber *charactersRemaining

    Discussion

    Set this property to a non-nil value to make the character count view appear in the compose view (changing the value redraws the character count view). The default value of this property is nil.

    Availability

    Available in iOS 8.0 and later.

  • A Boolean value that indicates whether the current content and attachments are valid.

    Declaration

    Swift

    func isContentValid() -> Bool

    Objective-C

    - (BOOL)isContentValid

    Return Value

    YEStrue if the current content and attachments are valid for posting, otherwise NOfalse. The default return value is YEStrue.

    Discussion

    This method is automatically called after each change a user makes to the text in the compose view. You can use this method to determine whether the Post button should be enabled and to update charactersRemaining, if necessary.

    A subclass should implement this method to perform custom validation of the user’s content before initiating a post.

    Availability

    Available in iOS 8.0 and later.

  • Performs validation of the current content and updates the state of the Post button, if appropriate.

    Declaration

    Swift

    func validateContent()

    Objective-C

    - (void)validateContent

    Discussion

    By default, validateContent calls isContentValid, performs internal content validation, and updates the state of the Post button, if necessary. You should call this method if you change any data that your implementation of isContentValid uses to test for content validity.

    Availability

    Available in iOS 8.0 and later.

  • Loads a view that displays a preview of the attachments in the extension context.

    Declaration

    Swift

    func loadPreviewView() -> UIView!

    Objective-C

    - (UIView *)loadPreviewView

    Return Value

    A view that can appropriately display a preview of the attachments in the context, or nil if a preview is unnecessary for the context.

    Discussion

    A preview view appears next to the text editing area in a compose view. A subclass can override this method to provide a custom preview view.

    Availability

    Available in iOS 8.0 and later.

  • Returns configuration items to display in the compose view.

    Declaration

    Swift

    func configurationItems() -> [AnyObject]!

    Objective-C

    - (NSArray *)configurationItems

    Return Value

    An array of SLComposeSheetConfigurationItem objects, or nil if no configuration items need to be displayed.

    Discussion

    Implement this method if you need to display configuration items, such as an account picker or privacy indicator, in your compose view.

    Availability

    Available in iOS 8.0 and later.

  • Dismisses the current configuration view controller.

    Declaration

    Swift

    func popConfigurationViewController()

    Objective-C

    - (void)popConfigurationViewController

    Discussion

    You can call this method to dismiss the configuration view controller in response to a user action.

    Availability

    Available in iOS 8.0 and later.

  • Presents a configuration view controller that lets the user configure the post.

    Declaration

    Swift

    func pushConfigurationViewController(_ viewController: UIViewController!)

    Objective-C

    - (void)pushConfigurationViewController:(UIViewController *)viewController

    Parameters

    viewController

    The view controller that manages the type of configuration the user selected.

    Discussion

    Typically, this method is called in the tap handler for a configuration item. A user selects a configuration item from the list displayed in the compose view and the associated configuration view controller is displayed. Only one configuration view controller can be visible at a time.

    Note that your custom configuration view controller should set its preferredContentSize property to an appropriate value. SLComposeServiceViewController observes changes to the preferredContentSize property and animates view size changes if necessary.

    Availability

    Available in iOS 8.0 and later.

  • Reloads the list of configuration items.

    Declaration

    Swift

    func reloadConfigurationItems()

    Objective-C

    - (void)reloadConfigurationItems

    Discussion

    In general, a subclass doesn’t need to call this method unless it determines its configuration items in a deferred way, such as in presentationAnimationDidFinish. In particular, you don’t need to call this method after you change a configuration item property, because the SLComposeServiceViewController base class automatically detects and responds to such changes.

    Availability

    Available in iOS 8.0 and later.

  • The view controller that manages an autocompletion view for suggesting common text completions while users type.

    Declaration

    Swift

    var autoCompletionViewController: UIViewController!

    Objective-C

    @property(strong, nonatomic) UIViewController *autoCompletionViewController

    Discussion

    An autocompletion view can appear in place of the list of configuration items, just below the text view in the compose view. The compose view controller allows only one autocompletion view controller to be present at a time.

    Note that your custom autocompletion view controller should set its preferredContentSize property to an appropriate value. SLComposeServiceViewController observes changes to the preferredContentSize property and animates view size changes if necessary.

    Availability

    Available in iOS 8.0 and later.

  • A string that represents the text the user entered into the compose view’s text view. (read-only)

    Declaration

    Swift

    var contentText: String! { get }

    Objective-C

    @property(readonly, nonatomic) NSString *contentText

    Availability

    Available in iOS 8.0 and later.

  • A string that’s displayed in the compose view’s text view when the text view is empty.

    Declaration

    Swift

    var placeholder: String!

    Objective-C

    @property(copy, nonatomic) NSString *placeholder

    Availability

    Available in iOS 8.0 and later.

  • The editable text view in the compose view. (read-only)

    Declaration

    Swift

    var textView: UITextView! { get }

    Objective-C

    @property(readonly, nonatomic) UITextView *textView

    Discussion

    When a user activates a Post or Send button, you can send their text by sending self.textView.text. Note that the SLComposeServiceViewController base class creates textView in its loadView method and sets itself to be the textView delegate.

    Availability

    Available in iOS 8.0 and later.