AirPrint Tutorial in iOS8 with Swift

With Airprint-enabled printers it is possible to print wirelessly from an iPhone or an iPad. In this tutorial we will print the text of a Text View. This tutorial is built in iOS 8.1 with Xcode 6.1.

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

To test the AirPrint ability we need to install the Printer Simulator. Go to and download Hardware IO Tools for Xcode.

Open the Printer Simulator. The Simulator has some virtual printers included which we can use in our application.

Go to the Storyboard and drag a Text View to the main view. Also drag a button to the main view and place it below the Text View. Set the button's title to Print. The storyboard should look like this.

Select both the Text View and the Button and click the "Resolve Auto Layout Issues" button. (3rd button on the bottom-right of the Storyboard).

Select the Assistant Editor and make sure the ViewController.swift is visible. Ctrl and Drag from the Text View to the ViewController class and create the following Outlet.

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

Implement the printText method.

@IBAction func printText(sender: UIButton) {
    // 1
    let printController = UIPrintInteractionController.sharedPrintController()!
    // 2
    let printInfo = UIPrintInfo(dictionary:nil)!
    printInfo.outputType = UIPrintInfoOutputType.General
    printInfo.jobName = "print Job"
    printController.printInfo = printInfo
    // 3
    let formatter = UIMarkupTextPrintFormatter(markupText: textView.text)
    formatter.contentInsets = UIEdgeInsets(top: 72, left: 72, bottom: 72, right: 72)
    printController.printFormatter = formatter
    // 4
    printController.presentAnimated(true, completionHandler: nil)
  1. The UIPrintInteractionController presents the user interface and manages the printing.
  2. The UIPrintinfo object contains information about the print job, This information is assigned to the printInfo property of the UIPrintInteractionController.
  3. The printing text can be formatted. here we define the insets for the printing page
  4. The user is presented the iPhone printing interface to select a printer and the number of copies

Build and Run the Project and press the Print Button. The Printing interface appears.

Select a printer and press the Print button. On a real device the printing should start. In the iOS simulator a Preview is shown. The yellow edges are the edge insets.

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