Integrating Twitter into iPad iOS 5 Applications

From Techotopia
Jump to: navigation, search
PreviousTable of ContentsNext
Recording Audio on an iPad with AVAudioRecorderAn Example iPad iOS 5 TWTweetComposeViewController Twitter Application


Purchase the fully updated 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


Arguably one of the leading communications platforms in the burgeoning social media arena, Twitter currently has approximately 100 million registered users. Included amongst the many new features added to the iOS 5 SDK is the Twitter Framework, the sole purpose of which is to facilitate the integration of twitter capabilities into iOS applications.

Whether or not to integrate Twitter functionality into an iPad application is certainly a decision that should be given careful consideration by application developers. The good news, however, is that in the event that Twitter support is a logical extension to an iPad application, Apple has made basic Twitter integration remarkably easy.




The iOS 5 Twitter Framework

The iOS 5 Twitter Framework is comprised of two classes designed to facilitate the integration of Twitter functionality into iOS applications. The TWRequest class is provided for developers with specific needs in terms of Twitter integration and who are familiar with, or prepared to learn the Twitter API. In essence, the TWRequest class allows iOS applications to interact with the Twitter API through HTTP based requests.

In recognition of the relative complexities inherent in using TWRequest and the Twitter API, the Twitter Framework also includes the TWTweetComposeViewController class. This class provides the application developer with a pre-built user interface screen (referred to internally at Apple as the “tweet sheet”) designed to compose and send a messages using Twitter. This is the recommended mechanism for Twitter integration in situations where the flexibility offered by TWRequest is not needed and is the topic of the remainder of this chapter.

iOS 5 Accounts Framework

In addition to the Twitter Framework, iOS 5 also introduced the Accounts Framework, the purpose of which 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 5 at least, this is limited solely to Twitter accounts though this will likely be extended to other platforms such as Facebook in future iOS releases.

Using the Accounts framework, code can be written to access, create and validate Twitter accounts on iOS 5 based devices.

The iOS 5 operating system running on iPhone and iPad devices is able to store multiple Twitter accounts which 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. Any pre-configured Twitter accounts will be listed together with a button providing the option to add a new Twitter account:


The iPad iOS 5 Twitter settings page

Figure 61-1


When using the TWRequest class to construct Twitter API requests it will be necessary to use the Accounts framework to identify and request permission to use Twitter accounts in an application. When using the TWTweetComposeViewController, however, the account handling is performed automatically by the class.


The TWTweetComposeViewController Class

As previously discussed, the TWTweetComposeViewController class presents the user with a screen that can be used to send a Twitter message. Figure 61-2 provides an illustration of the class in action within an iPad application:


An example of the iOS 5 "Tweet Sheet" twitter composer panel on an iPad

Figure 61-2


In order to use the TWTweetComposeViewController class a number of steps should be performed in sequence. Firstly, the application may optionally check to verify whether a Twitter message can be sent using the device. This essentially equates to checking if a Twitter account has been configured on the device and is achieved using the canSendTweet class method as follows:

if ([TWTweetComposeViewController canSendTweet]) {
   // Device is able to send a Twitter message
}

This method call is optional and, in the event that a Twitter account 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 TWTweetComposeViewController 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 Twitter message on the TWTweetComposeViewController instance.
  • addImage: - Adds image files as attachments to the message. The class automatically handles URL shortening and resizing of the image for Twitter image hosting compatibility.
  • addURL: - Adds a URL to the Twitter 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. A return value indicating failure typically means either that the Twitter 140 character limit has been exceeded (keep in mind that URLs and images in the message use up characters) or that the composer screen has already been presented to the user (programmatically changing the content of the message after the screen has been displayed is disallowed).

Finally, when the message is ready to be presented to the user, the TWTweetComposeViewController object is presented modally by calling the presentModalViewController: method of the parent view controller:

[self presentModalViewController:tweetView animated: YES];

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, change the Twitter account via which the message is to be sent (assuming more than one account has been configured on the device) 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:

  • TWTweetComposeViewControllerResultCancelled – The user cancelled the Twitter composition session by touching the Cancel button.
  • TWTweetComposeViewControllerResultDone – The user sent the composed Twitter message by touching the Send button.

Summary

iOS 5 introduced both the Twitter and Accounts frameworks both of which may be used to integrate Twitter functionality into iOS 5 applications. For more complex requirements the TWRequest class may be used in conjunction with the Accounts framework to access Twitter accounts and construct and submit Twitter API HTTP requests. An alternative and simpler approach that will meet the needs of most application developers involves the use of the TWTweetComposeViewController class. This chapter provided an overview of this class and the steps necessary to deploy it in applications. The next chapter, entitled An Example iPad iOS 5 TWTweetComposeViewController Twitter Application will work through the development of an example application using the TWTweetComposeViewController class to implement basic Twitter functionality.


Purchase the fully updated 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
Recording Audio on an iPad with AVAudioRecorderAn Example iPad iOS 5 TWTweetComposeViewController Twitter Application