The iOS 6 UIActivityController class
Integration of social networks into iOS 6 applications is performed through the use of either the UIActivityViewController class, or the classes of the new Social Framework of the iOS 6 SDK. At the time of writing, social networks supported by these classes consist of Facebook, Twitter and Sina Weibo, though it is probable that more will follow in future SDK releases.
For general purpose social network integration, the UIActivityViewController class is the recommended path. When using this class, the user is presented with a screen providing a choice of social network services to which a message may be posted (together with options to print or post via the built-in Mail or Message apps). Once the user of the application has selected a target service, the class then presents the user with a message preview panel where the message may be reviewed and modified prior to being sent. Once the message has been posted, the class handles all aspects of connecting to the user’s chosen social network and subsequently posting the update to that service.
The Social Framework
The iOS 6 Social Framework contains two classes designed to provide more flexible and general purpose mechanisms for social network integration.
The SLComposeViewController class, unlike the UIActivityViewController class, allows a post to be targeted to a specific social network service within the application code, without requiring the user to make a selection from those services available. The target service is simply specified at the point that an instance of the class is created within the application code. The user is then presented with a preview sheet appropriate to the specified service.
The SLRequest class of the Social Framework, on the other hand, is provided for developers with specific needs in terms of social network integration and who are familiar with, or prepared to learn, the corresponding social network APIs. In essence, the SLRequest class allows iOS applications to interact directly with social network APIs through HTTP based requests.
iOS 6 Accounts Framework
The purpose of the Accounts Framework is to provide access to device based system accounts from within iOS applications. The term system accounts is somewhat misleading since, in the case of the first release of iOS 6 at least, this is limited solely to Facebook, Twitter and Sina Weibo accounts, though this will likely be extended to other platforms in future iOS releases. Using the Accounts framework, code can be written to access, create and validate social network accounts stored on iOS 6 based devices.
The iOS 6 operating system running on iPhone and iPad devices is able to store multiple Twitter accounts but only one Facebook account at a time. These are accessible both from the device Settings application in addition to using the Accounts Framework within application code.
To manually view and configure Twitter accounts on a device, select the Settings application and choose the Twitter option. As illustrated in Figure 3 1, any pre-configured Twitter accounts will be listed together with a button providing the option to add a new Twitter account and switches providing control over which applications may be granted permission to access those Twitter accounts:
The current Facebook account settings on the device may, similarly, be viewed and modified by selecting the Facebook option of the main screen of the Settings application. Options are also provided on the Facebook settings panel to control which applications have access to the Facebook account, and to modify or delete the currently configured Facebook account.
When using the SLRequest class to construct API requests, it will be necessary to use the Accounts framework to identify and request permission to use the corresponding social network accounts in an application. When using the UIActivityViewController or SLComposeViewController, however, the account handling is performed automatically by the class. The remainder of this chapter will focus on the UIActivityViewController and SLComposeViewController classes. The SLRequest class will be covered in detail in the chapter entitled iPhone iOS 6 Facebook and Twitter Integration using SLRequest.
Using the UIActivityViewController Class
The UIActivityViewController class is instantiated within an application at the point at which a posting is ready to be made to a social network. Once the user takes an action to post an update from within an application, the screen shown in Figure 3-2 will appear listing the options that are available:
Once a destination social network has been selected, the preview sheet for the chosen service (Figure 3-3 shows the sheet for a Facebook posting) will appear primed with the content of the update to be sent consisting of text and any optional image attachments. Having reviewed and, optionally, modified the post, user may then send the message.
In the event that the user has not yet configured an account for the selected social network in the Settings application, a dialog will appear providing the option to either set up an account or cancel the post.
The assumption is generally made that at the point an instance of UIActivityViewController is created, the application will already have gathered together the text and images that are to be included in the post. These items need to be placed into an NSArray object and passed to the controller before it is presented to the user. The following code excerpt, for example, instantiates a UIActivityViewController instance primed with text and an image to be included in the post and presents it to the user: NSString *postText = @”My first Facebook post from iOS 6”;
UIImage *postImage = [UIImage imageNamed:@”myImage.png”];
NSArray *activityItems = @[postText, postImage];
UIActivityViewController *activityController =
Using the SLComposeViewController Class
In order to use the SLComposeViewController class, a number of steps should be performed in sequence. Firstly, the application may optionally check to verify whether a message can be sent to the specified social network service. This essentially equates to checking if a valid social network account has been configured on the device and is achieved using the isAvailableForServiceType: class method, passing through as an argument the type of service required from the following options:
The following code, for example, verifies that Twitter service is available to the application:
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
// Device is able to send a Twitter message
This method call is optional and, in the event that an account for the specified social network has yet to be set up, the composer will simply take the user to the device’s Settings application where a Twitter account may be configured.
The next step is to create an instance of the SLComposeViewController class and supply an optional completion handler to be called when the composer screen is either cancelled by the user or used to send a message. Next, a range of methods may be called on the instance to initialize the object with the content of the message, including the initial text of the message, an image attachment and a URL:
- setInitialText: – Sets the initial text of the message on the SLComposeViewController instance.
- addImage: – Adds image files as attachments to the message.
- addURL: – Adds a URL to the message. The method automatically handles the URL shortening.
Each of the above methods returns a Boolean result indicating whether the addition of content was successful.
Finally, when the message is ready to be presented to the user, the SLComposeViewController object is presented modally by calling the presentViewController: method of the parent view controller.
The following code excerpt demonstrates the steps to create, configure and present a typical SLComposeViewController instance for posting to Facebook:
SLComposeViewController *composeController = [SLComposeViewController
[composeController setInitialText:@"Just found this great website"];
[composeController addURL: [NSURL URLWithString:
Once called, this method will present the composer view to the user primed with any text, image and URL contents pre-configured via the method calls. Once displayed, the user has the option to modify the text of the message, cancel the message, add location data or send the message. If a completion handler has been configured it will be called and passed a value indicating the action that was taken by the user within the composer view. Possible values are:
- SLComposeViewControllerResultCancelled – The user cancelled the composition session by touching the Cancel button.
- SLComposeViewControllerResultDone – The user sent the composed message by touching the Send button.
iOS 6 introduced both the UIActivityViewController class and Social Framework, both of which may be used to integrate Twitter, Facebook and Sina Weibo functionality into iOS 6 applications. For general purpose requirements, the UIActivityViewController and SLComposeViewController classes provide an easy to implement path to social network integration. For more complex requirements, however, the SLRequest class may be used in conjunction with the Accounts framework to access social network accounts and construct and submit HTTP requests to supported social network APIs.
This chapter provided an overview of the UIActivityViewController and SLComposeViewController classes and outlined the steps necessary to deploy them in applications. The next chapter, entitled An iPhone iOS 6 Facebook Integration Tutorial using UIActivityViewController will work through the development of an example application using the UIActivityViewController class to implement Facebook integration into an iOS 6 based iPhone app. The concepts behind the SLRequest class will be covered in iPhone iOS 6 Facebook and Twitter Integration Tutorial using SLRequest.