Twitter Tutorial in iOS8 with Swift

With the Social Framework it is possible to use social media from inside an App. In this tutorial we will pick an image from the Photo Library and tweet a message including the image to Twitter. This tutorial is built with Xcode 6 and IOS8.

Update: Mar 14, 2017. The rewritten version in Swift for iOS 10.2 and Xcode 8.2.1 is available here.

Open Xcode and create a new Single View Application. For product name, use IOS8SwiftTwitterTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.

Go to the Storyboard. In Xcode 6 view controllers are now square by default, so that you can create one user interface for multiple devices. In iOS8 you can layout the Storyboard using constraints and size classes. Since this app is only going to be for the iPhone, you can disable size classes. In the File Inspector untick Use Size Classes.

Choose Keep size class data for: iPhone, and click Disable Size Classes.

Drag an Image View to the main View.  and two buttons to the view. Select the Image View and go to the Attributes inspector. In the View section give the view a mode of "Aspect Fit".

This mode will fit the image into the Image View. Next, add two Buttons to the view and place them below the Image View. Give them a title of "Choose Image" and "Tweet" . The Storyboard should look like this.

Open the Assistant Editor and make sure the ViewController.swift file is visible. Ctrl + drag from the Image View to the ViewController class and create the following Outlet.

Ctrl + drag from the Choose Image Button to the ViewController class and create the following Action.

Ctrl + drag from the Choose Image Button to the ViewController class and create the following Action.

To use the Social framework we will have to import it to the project. Add the following line to the top of the ViewController.swift file.

import Social

First, we will implement the chooseImagePressed method to create the tweet sheet.

@IBAction func tweetButtonPressed(sender: UIButton) {
  // 1
  if SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter) {
    // 2
    var tweetSheet = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
      // 3
      tweetSheet.setInitialText("Look at this nice picture!")
      // 4
      self.presentViewController(tweetSheet, animated: true, completion: nil)
    } else {
      // 5

Let's take a look at this method

  1. We check if there is a twitter account available on the device.
  2. We create a SLComposeViewController object, which embeds the tweet sheet and all functionality.
  3. The initial tweet text is set, also the selected image is added to the tweet.
  4. The tweet sheet is presented.
  5. If the twitter account isn't set up, we display an alert message at the console

We now must  implement the ability to show the Photo Library of the device. This is made possible with the imagePickerController object. Our view controller must conform to the UIImagePickerControllerDelegate and UINavigationControllerDelegate protocol. Change the class declaration line to

class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {

Next, create a property which initializes an UIImagePickerController

var pickerController:UIImagePickerController = UIImagePickerController()

Next, implement the chooseImagePressed method

@IBAction func chooseImagePressed(sender: UIButton) {
  // 1  
  pickerController.delegate = self  
  pickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
  // 2  
  self.presentViewController(pickerController, animated: true, completion: nil)
  1. Set the delegate to the View Controller and set the sourceType to the Photo Library
  2. Present the Image Picker Controller.

Finally, we must implement the imagePickerController:didFinishPickingMediaWithInfo method. Here we assign the chosen image from the library to the image view.

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
    imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
    self.dismissViewControllerAnimated(true, completion: nil)

To put some images to the Photo Library you can drag some example image to the iOS Simulator. You also need to create a Twtitter account in the Settings of the iOS Simulator. 

Build and Run the project. The application will ask for permission to access the Photo Library, which you need to Allow.

Next, Press the "Choose Image" button and select a picture from the Photo Library. Next, press the tweet button to create and send the tweet.

You can download the source code of the iOS8SwiftTutorial at the ioscreator repository on github