Difference between revisions of "Integrating Twitter and Facebook into iOS 8 Applications using Swift"
m (Text replacement - "<hr> <table border=" to "<htmlet>ezoicbottom</htmlet> <hr> <table border=") |
m (Text replacement - "<table border="0" cellspacing="0">" to "<table border="0" cellspacing="0" width="100%">") |
||
Line 1: | Line 1: | ||
− | <table border="0" cellspacing="0"> | + | <table border="0" cellspacing="0" width="100%"> |
<tr> | <tr> | ||
<td width="20%">[[Recording Audio on iOS 8 with AVAudioRecorder in Swift|Previous]]<td align="center">[[iOS 8 App Development Essentials|Table of Contents]]<td width="20%" align="right">[[An iOS 8 Swift Facebook Integration Tutorial using UIActivityViewController|Next]]</td> | <td width="20%">[[Recording Audio on iOS 8 with AVAudioRecorder in Swift|Previous]]<td align="center">[[iOS 8 App Development Essentials|Table of Contents]]<td width="20%" align="right">[[An iOS 8 Swift Facebook Integration Tutorial using UIActivityViewController|Next]]</td> | ||
Line 142: | Line 142: | ||
<htmlet>ezoicbottom</htmlet> | <htmlet>ezoicbottom</htmlet> | ||
<hr> | <hr> | ||
− | <table border="0" cellspacing="0"> | + | <table border="0" cellspacing="0" width="100%"> |
<tr> | <tr> | ||
<td width="20%">[[Recording Audio on iOS 8 with AVAudioRecorder in Swift|Previous]]<td align="center">[[iOS 8 App Development Essentials|Table of Contents]]<td width="20%" align="right">[[An iOS 8 Swift Facebook Integration Tutorial using UIActivityViewController|Next]]</td> | <td width="20%">[[Recording Audio on iOS 8 with AVAudioRecorder in Swift|Previous]]<td align="center">[[iOS 8 App Development Essentials|Table of Contents]]<td width="20%" align="right">[[An iOS 8 Swift Facebook Integration Tutorial using UIActivityViewController|Next]]</td> |
Revision as of 20:00, 27 October 2016
Previous | Table of Contents | Next |
Recording Audio on iOS 8 with AVAudioRecorder in Swift | An iOS 8 Swift Facebook Integration Tutorial using UIActivityViewController |
Learn SwiftUI and take your iOS Development to the Next Level |
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 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 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.
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 8 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 90-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:
Figure 90-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 iOS 8 Facebook and Twitter Integration using SLRequest and Swift.
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 90-2 will appear listing the options that are available:
Figure 90-2
Once a destination social network has been selected, the preview sheet for the chosen service (Figure 90-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.
Figure 90-3
Learn SwiftUI and take your iOS Development to the Next Level |
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:
let postText = "Great to finally be here on vacation!" let postImage = UIImage(named: "windsor_castle.jpg") let activityItems = [postText, postImage] let activityController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil) self.presentViewController(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. 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:
- SLServiceTypeFacebook
- SLServiceTypeTwitter
- SLServiceTypeSinaWeibo
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.
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:
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.presentViewController(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 8 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 8 Swift 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 app. The concepts behind the SLRequest class will be covered in iOS 8 Facebook and Twitter Integration using SLRequest and Swift.
Learn SwiftUI and take your iOS Development to the Next Level |
Previous | Table of Contents | Next |
Recording Audio on iOS 8 with AVAudioRecorder in Swift | An iOS 8 Swift Facebook Integration Tutorial using UIActivityViewController |