Activity View Controller Tutorial in iOS8 with Swift

The Activity View Controller  is a standard view controller that you can use to offer various services from your application. The system provides several standard services, such as copying items to the pasteboard, posting content to social media sites, sending items via email or SMS, and more. In this tutorial, we will share some text input from a text field using a activity view controller. This tutorial is built in iOS8.1 and Xcode 6.1

Update: Apr 22, 2015. This tutorial is updated for Swift 1.2 and Xcode 6.3.

Open Xcode and create a new Single View Application. For product name, use iOS8SwiftActivityViewControllerTutorial 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 use just 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.

First, let's create the user interface, Go to the storyboard and drag a text field and a button to the View Controller. Set the title of the button to share. The storyboard should look like this

Select the Assistant Editor and open ViewController.swift. Ctrl and drag from the text field to the ViewController class and create the following outlet

Ctrl and drag from the button to the ViewController class and create the following outlet

Repeat the preceding procedure, but now create an action

If we would run the project now, there is no way to dismiss the keyboard when editing our Text Field. Let's fix this first. Make our viewController comform to the UITextFieldDelegate protocol by changing the class line to

class ViewController: UIViewController,UITextFieldDelegate {

Next, add the following line add the end of the viewDidLoad method

textField.delegate = self

When the user is in editing mode the keyboard should be dismissed when the user press return. This can be accomplished by creating the textFieldShouldReturn method

func textFieldShouldReturn(textField: UITextField) -> Bool {
    return true

Now it is time to take a look at our activity view controller. First, create the following variable. 

var activityViewController:UIActivityViewController?

Next, implement the shareText method

@IBAction func shareText(sender: UIButton) {
    let activityViewController = UIActivityViewController(
      activityItems: [textField.text as NSString],
      applicationActivities: nil)
    presentViewController(activityViewController, animated: true, completion: nil)

the Activity View Controller is initialized with an array of activityItems. We use the textfield's text as the only activity item. The Activity View Contoller is presented and the sharing items will be displayed.

Build and Run the project, enter some text and press the Share button. If you run the app on a device more sharing options will be presented.

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