Take Video Tutorial in iOS8 with Swift

Apple provides the UIImagePickerController which is an user interface to take videos using the built-in camera of an iOS device. In this tutorial we will take a video which will be saved in the Photo Library of the device. This tutorial is built in iOS 8.4 and Xcode 6.4

Open Xcode and create a new Single View Application. For product name, use IOS8SwiftTakeVideoPlayerTutorial 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 and drag two buttons from the Object Library to the main view. Give the buttons a title of "Take Video" and "View Library". The storyboard should look like this.

Hold down the Ctrl key and select both buttons. Click the "Resolve Auto Layout Issues" button on the bottom-right of the Storyboard and select "Add Missing Constraints".

Select the Assistant Editor and make sure the ViewController.swift is visible. Ctrl and drag from the top Button and create the following Action.

Ctrl and drag from the bottom Button and create the following Action.

Go to the ViewController.swfit file and add the following lines at the top off the file.

import MobileCoreServices
import AssetsLibrary

Change the ViewController class declaration line to

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

These delegates are needed to let the ViewController handle the UIImagePickerController delegation. Implement the takeVideo method

@IBAction func takeVideo(sender: AnyObject) {
    // 1 Check if project runs on a device with camera available
    if UIImagePickerController.isSourceTypeAvailable(.Camera) {
        // 2 Present UIImagePickerController to take video
        controller.sourceType = .Camera
        controller.mediaTypes = [kUTTypeMovie as! String]
        controller.delegate = self
        controller.videoMaximumDuration = 10.0
        presentViewController(controller, animated: true, completion: nil)
     else {
        println("Camera is not available")
  1. The isSourceTypeAvailable method checks if the device supports the Camera sourceType
  2. An ImagePickerController is displayed using the Camera Sourcetype and the Movie mediaType. The maximum length of a movie is set to 10 seconds,

Implement the viewLibrary method.

@IBAction func viewLibrary(sender: AnyObject) {
            // Display Photo Library
            controller.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
            controller.mediaTypes = [kUTTypeMovie as! String]
            controller.delegate = self
            presentViewController(controller, animated: true, completion: nil)

The Photo library is displayed. If the mediaType isn't set to the movie type, the video files will not be displayed. Next, the delegate methods of the UIImagePickerControllerDelegate protocol needs to be implemented.

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject: AnyObject]) {
    // 1    
    let mediaType:AnyObject? = info[UIImagePickerControllerMediaType]
    if let type:AnyObject = mediaType {
        if type is String {
            let stringType = type as! String
                if stringType == kUTTypeMovie as! String {
                    let urlOfVideo = info[UIImagePickerControllerMediaURL] as? NSURL
                        if let url = urlOfVideo {
                            // 2  
                                completionBlock: {(url: NSURL!, error: NSError!) in
                                        if let theError = error{
                                            println("Error saving video = \(theError)")
                                        else {
                                            println("no errors happened")
    // 3
    picker.dismissViewControllerAnimated(true, completion: nil)

The imagePickerController(_:didFinishPickingMediaWithInfo:) method tells the delegate the user picked a movie. The info parameter contains the URL of the picked movie.

  1. The mediatype from the info dictionary is checked if it is a movie type. If this is the case the URL of the video is extracted.
  2. The writeVideoAtPathToSavedPhotosAlbum method saves the video into the photo album
  3. The ViewController is dismissed

Implement the imagePickerControllerDidCancel method

func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        picker.dismissViewControllerAnimated(true, completion: nil)

When the user press the Cancel button, the View Controller is dismissed. Build and Run the project on a real device, since the Simulator doesn't have a camera. Choose "Take Video" to shoot the video and click Use Video. Next, choose "View Library" and the video appears in the library.

You can download the source code of the IOS8SwiftTakeVideoPlayerTutorial at the ioscreator repository on Github.