Integrating Twitter and Facebook into iOS 10 Applications

From Techotopia
Jump to: navigation, search

PreviousTable of ContentsNext
An iOS 10 SiriKit Photo Search TutorialAn iOS 10 Facebook Integration Tutorial using UIActivityViewController


You are reading a sample chapter from the iOS 10 App Development Essentials book.

Purchase the full iOS 10 / Swift 3 / Xcode 8 edition of this book in eBook ($19.99) or Print ($45.99) format.
iOS 10 App Development Essentials Print and eBook (ePub/PDF/Kindle) edition contains over 100 chapters. Learn more...

Buy eBook Buy Print Preview Book


Social networking services are, for better or for worse, becoming an increasingly prominent aspect of our daily lives. Apple provides support for integrating social elements into iOS apps via the Social Framework with support included for Twitter, Facebook, Tencent Weibo and Sina Weibo.

Within this chapter, the basics of Twitter and Facebook integration will be covered.




The UIActivityViewController class

Integration of social networks into iOS applications is performed through the use of either the UIActivityViewController class, or the classes of the Social Framework of the iOS SDK. At the time of writing, social networks supported by these classes consist of Facebook, Twitter, Tencent Weibo 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 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.

You are reading a sample chapter from the iOS 10 App Development Essentials book.

Purchase the full iOS 10 / Swift 3 / Xcode 8 edition of this book in eBook ($19.99) or Print ($45.99) format.
iOS 10 App Development Essentials Print and eBook (ePub/PDF/Kindle) edition contains over 100 chapters. Learn more...

Buy eBook Buy Print Preview Book


Accounts Framework

The purpose of the Accounts Framework is to provide access to device based system accounts from within iOS applications. Using the Accounts framework, code can be written to access, create and validate social network accounts stored on iOS based devices.

The iOS 10 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 102-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:


Ios 8 twitter settings.png

Figure 102-1


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 classes, 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 An iOS 10 Twitter Integration Tutorial 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 102-2 will appear listing the options that are available:


Ios 8 select social option.png

Figure 102-2


Once a destination social network has been selected, the preview sheet for the chosen service (Figure 102-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, the user may then send the message.


Ios 8 facebook activity controller.png

Figure 102-3


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 array 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:

You are reading a sample chapter from the iOS 10 App Development Essentials book.

Purchase the full iOS 10 / Swift 3 / Xcode 8 edition of this book in eBook ($19.99) or Print ($45.99) format.
iOS 10 App Development Essentials Print and eBook (ePub/PDF/Kindle) edition contains over 100 chapters. Learn more...

Buy eBook Buy Print Preview Book

let postText = "Great to finally be here on vacation!"
let postImage = UIImage(named: "windsor_castle.jpg")
let activityItems = [postText as AnyObject, postImage]

let activityController = UIActivityViewController(activityItems: 
			activityItems, applicationActivities: nil)

self.present(activityController, animated: true,
                completion: nil) 

Using the SLComposeViewController Class

In order to use the SLComposeViewController class, a number of steps should be performed in sequence. First, 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:

• SLServiceTypeFacebook

• SLServiceTypeTwitter

• SLServiceTypeSinaWeibo

• SLServiceTypeTencentWeibo

The following code, for example, verifies that the 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.

You are reading a sample chapter from the iOS 10 App Development Essentials book.

Purchase the full iOS 10 / Swift 3 / Xcode 8 edition of this book in eBook ($19.99) or Print ($45.99) format.
iOS 10 App Development Essentials Print and eBook (ePub/PDF/Kindle) edition contains over 100 chapters. Learn more...

Buy eBook Buy Print Preview Book

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 present 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:

let composeController = SLComposeViewController(forServiceType: 
					SLServiceTypeFacebook)

composeController.setInitialText("Just found this great web site")
composeController.addImage(UIImage(named: "windsor_castle.jpg"))
composeController.addURL(NSURL(string: "http://www.ebookfrenzy.com"))

self.present(composeController, 
			animated: true, completion: nil)

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:

SLComposeViewControllerResult.cancelled – The user cancelled the composition session by touching the Cancel button.

SLComposeViewControllerResult.done – The user sent the composed message by touching the Send button.

Summary

The iOS 10 SDK includes both the UIActivityViewController class and Social Framework, both of which may be used to integrate Twitter, Facebook and Sina Weibo functionality into iOS applications.

For general purpose requirements, both the UIActivityViewController and the 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 iOS 10 Social Media Integration Tutorial using UIActivityViewController will work through the development of an example application using the UIActivityViewController class to implement Facebook integration into an iOS app. The concepts behind the SLRequest class will be covered in An iOS 10 Twitter Integration Tutorial using SLRequest.


You are reading a sample chapter from the iOS 10 App Development Essentials book.

Purchase the full iOS 10 / Swift 3 / Xcode 8 edition of this book in eBook ($19.99) or Print ($45.99) format.
iOS 10 App Development Essentials Print and eBook (ePub/PDF/Kindle) edition contains over 100 chapters. Learn more...

Buy eBook Buy Print Preview Book



PreviousTable of ContentsNext
An iOS 10 SiriKit Photo Search TutorialAn iOS 10 Facebook Integration Tutorial using UIActivityViewController