SLComposeViewController Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/Social.framework
Availability
Available in iOS 6.0 and later.
Declared in
SLComposeViewController.h

Overview

The SLComposeViewController class presents a view to the user to compose a post for supported social networking services.

Use the isAvailableForServiceType: class method to check if a service account, such as Twitter, is set up and reachable before presenting this view to the user.

Set the initial content before presenting the view controller to the user. All the methods that set the content of a post return a Boolean value. They return NO if the content doesn’t fit in the post or if the view controller has already been presented to the user. You must set all of the content in the post before presenting the view controller to the user. After presenting the view controller, only the user can edit the post.

You can set a handler—using the completionHandler property—to be notified when the user is done composing a post. Note that completion handlers are not called on any particular thread.

Tasks

Creating a Social Compose View Controller

Checking the Social Service Type

Composing Posts

Handling Results

Properties

completionHandler

The handler to call when the user is done composing a post.

@property (nonatomic, copy) SLComposeViewControllerCompletionHandler completionHandler
Discussion

The handler has a single parameter that indicates whether the user finished or cancelled composing the post. This block is not guaranteed to be called on any particular thread.

Special Considerations

In iOS 6 and earlier, if you set a completion handler then your completion handler is responsible for dismissing the SLComposeViewController using dismissViewControllerAnimated:completion:. In iOS 7 and later when compiled with the iOS 7 or later SDK you must not dismiss the SLComposeViewController in your completion handler—the system will do so automatically.

Availability
  • Available in iOS 6.0 and later.
Declared In
SLComposeViewController.h

serviceType

Specifies the social networking service.

@property(nonatomic, readonly) NSString *serviceType
Discussion

The value of this property is set when you initialize a social compose view controller in composeViewControllerForServiceType:. Each social view controller you present is connected to only one social service at a time. Use this property to check which service your social view controller has specified. For a list of possible values, see Service Type Constants.

Availability
  • Available in iOS 6.0 and later.
Declared In
SLComposeViewController.h

Class Methods

composeViewControllerForServiceType:

Creates a new social compose view controller.

+ (SLComposeViewController *)composeViewControllerForServiceType:(NSString *)serviceType
Parameters
serviceType

This specifies the social networking service you want to post to. You must use one of the possible values listed in Service Type Constants. This also sets the value of serviceType. If an invalid serviceType is passed in, this method throws an exception.

Return Value

Returns a social compose view controller or nil if an error occurs.

Discussion

Use this method to create a social compose view controller. Do not use any other methods.

Availability
  • Available in iOS 6.0 and later.
Declared In
SLComposeViewController.h

isAvailableForServiceType:

Returns whether you can send a request for a particular service type.

+ (BOOL)isAvailableForServiceType:(NSString *)serviceType
Parameters
serviceType

The social networking service. For a list of possible values, see Service Type Constants.

Return Value

Returns a Boolean value indicating whether the service is accessible and at least one account is set up.

Discussion

For the account to be available, the user must be logged into the social service in the device settings.

Availability
  • Available in iOS 6.0 and later.
Declared In
SLComposeViewController.h

Instance Methods

addImage:

Adds an image to the post.

- (BOOL)addImage:(UIImage *)image
Parameters
image

The image to add to the post.

Return Value

Returns a Boolean value indicating whether the image was successfully added.

Discussion

This method returns NO if image does not fit in the currently available space or if the view controller has already been presented to the user (and therefore cannot be changed). For the accepted UIImage formats, see “Supported Image Formats” in UIImage Class Reference. Image size limits are dependent on the target service and are documented by the service provider. For links to documentation for the supported services, see “Table 1 Social Services Individual Documentation Sites” in SLRequest Class Reference.

Availability
  • Available in iOS 6.0 and later.
Declared In
SLComposeViewController.h

addURL:

Adds a URL to the post.

- (BOOL)addURL:(NSURL *)url
Parameters
url

The URL to add to the post.

Return Value

Returns a Boolean value indicating whether the URL was successfully added.

Discussion

This method returns NO if url does not fit in the currently available character space or if the view controller has already been presented to the user (and therefore cannot be changed). Character limits are dependent on the target service and are documented by the service provider. For links to documentation for the supported services, see “Table 1 Social Services Individual Documentation Sites” in SLRequest Class Reference.

Availability
  • Available in iOS 6.0 and later.
Declared In
SLComposeViewController.h

removeAllImages

Removes all images from the post.

- (BOOL)removeAllImages
Return Value

Returns a Boolean value indicating whether the images were successfully removed.

Discussion

If the view controller has already been presented to the user when removeAllImages is called, the method returns NO and the images are not removed.

Availability
  • Available in iOS 6.0 and later.
Declared In
SLComposeViewController.h

removeAllURLs

Removes all URLs from the post.

- (BOOL)removeAllURLs
Return Value

Returns a Boolean value indicating whether the URLs were successfully removed.

Discussion

If the view controller has already been presented to the user when removeAllURLs is called, the method returns NO and the URLS are not removed.

Availability
  • Available in iOS 6.0 and later.
Declared In
SLComposeViewController.h

setInitialText:

Sets the initial text to be posted.

- (BOOL)setInitialText:(NSString *)text
Parameters
text

The text to add to the post.

Return Value

Returns a Boolean value indicating whether the text was successfully set.

Discussion

This method returns NO if text does not fit in the currently available character space or if the view controller has already been presented to the user (and therefore cannot be changed). Character limits are dependent on the target service and are documented by the service provider. For links to documentation for the supported services, see “Table 1 Social Services Individual Documentation Sites” in SLRequest Class Reference.

Availability
  • Available in iOS 6.0 and later.
Declared In
SLComposeViewController.h

Constants

SLComposeViewControllerCompletionHandler

Defines a handler to call when the user finishes composing a post.

typedef void (^SLComposeViewControllerCompletionHandler)
(SLComposeViewControllerResult result);
Discussion

The completion handler is called while the SLComposeViewController is still visible and it is responsible for dismissing the view controller. For the possible values of the result parameter, see “SLComposeViewControllerResult.” Use the completionHandler property to set this handler.

Availability
  • Available in iOS 6.0 and later.
Declared In
SLComposeViewController.h

SLComposeViewControllerResult

Possible values for the result parameter of the completionHandler property.

typedef NS_ENUM(NSInteger,
   SLComposeViewControllerResult) {
   SLComposeViewControllerResultCancelled,
   SLComposeViewControllerResultDone
};
Constants
SLComposeViewControllerResultCancelled

The view controller is dismissed without sending the post. For example, the user selects Cancel or the account is not available.

Available in iOS 6.0 and later.

Declared in SLComposeViewController.h.

SLComposeViewControllerResultDone

The view controller is dismissed and the message is being sent in the background. This occurs when the user selects Done.

Available in iOS 6.0 and later.

Declared in SLComposeViewController.h.