iOS Tutorial: How to develop an iPhone app

This tutorial will show you how to:

Requirements

To develop iOS apps for the iPhone/iPad using the latest Xcode 6.3, you’ll need a Mac running the latest Mac OS X 10.10 Yosemite.

To run your apps on hardware devices, you need to be member in the iOS Developer Program. If you’re not a developer program member, you can only run your apps in the Simulator on the Mac.

Install Xcode and create a new iOS project

  1. Open the App Store app and install the latest Xcode (this tutorial was written for and tested with Xcode 6.3):

  2. Run Xcode and check that you’re using the latest Xcode version:

  3. Create a new project with File > New > Project (⌘⇧N). Select iOS > Application > Single View Application:

  4. Name the app “Beep”.
    Company Identifier should be a domain name (written the other way round). I’m going to use com.example, feel free to use your own domain name.
    Choose Swift as Language, select iPhone for Devices (for the moment, this will be an iPhone-only app).
    Uncheck Use Core Data (Core Data is a persistence/database framework, we don’t need it for this tutorial):

    New project settings

Add assets

  1. Download assets.zip and uncompress the archive.
    The icon Horn is created by Lloyd Humphreys from the Noun Project.
    The sound Trumpet is a sound from freesound.org.

  2. Open Images.xcassets in the Xcode project and drag in the images into the project:

Add a Button to the View Controller in the Storyboard

  1. Open Main.storyboard. You’ll see the Initial View Controller of the app with an empty View:

  2. Search for Button in the Object Library and drag it to the View:

  3. In the Attributes Inspector edit the properties for the Button: Remove the Title and select the Trumpet image:

Create Layout Constraints to center the Button

  1. Move the Button to the middle of the View and drag with Ctrl pressed down / with the right mouse key to the upper left to create horizontal and vertical layout constraints:

  2. With pressed Shift key select both Center Horizontally in Container and Center Vertically in Container and press Enter to confirm:

  3. This will create two layout constraints that make sure that the Button is always centered independent of the iPhone screen size:

Create an Action connection to handle tapping the Button

  1. Select the View Controller object and open the Identity Inspector. The Class specified here contains the code responsible for the managing the Views of this controller:

  2. Click with pressed down -key on the arrow:

    This opens the Swift code for the View Controller class in the Assistant Editor besides the Storyboard editor:

  3. Drag with Ctrl pressed down / with the right mouse key from the Button into the body of the class to create a new Action method that is called when the Button is tapped:

    Create an Action Connection

  4. Choose Action and name the method honk:

    Action Connection

Writing code to play a sound

  1. Drag the file trumpet.mp3 from the assets.zip download into the Xcode project:

    Drag In Project File

  2. Make sure to select Copy items if needed to copy the file to the Xcode project folder:

    Xcode Copy Items

  3. On the top of the ViewController.swift file, add an import to AVFoundation - this framework contains classes for audio and video handling:

    import UIKit
    import AVFoundation
  4. In the honk method, type avpl and press ⌘ Space to show the Xcode code completion:

    Xcode Code Completion

  5. Press Enter to confirm the AVPlayer selection.

  6. Click with on AVPlayer to show the Quick Help and select Class Reference to look up the documentation for the AVPlayer class:

    Class Reference

  7. Make yourself familiar with the methods to create an AVPlayer and to play a sound:

    Documentation

  8. Use the Xcode code completion to write code to load the trumpet sound from the app bundle into an AVPlayer, store it in the View Controller class and call its play-method when the button is tapped:

    class ViewController : UIViewController {
    
        let player = AVPlayer(URL: NSBundle.mainBundle().URLForResource("trumpet", withExtension: "mp3"))
        
        @IBAction func honk(sender: AnyObject) {
            player.play()
        }
        
    }

Running the app in the Simulator and on your iPhone

  1. Select an iPhone Simulator for the Scheme and hit Run (⌘R):

    Running in the simulator

  2. The app starts up in the Simulator:

    iPhone Simulator

  3. To run the app on the device, you need to be iOS Developer Program member.

  4. Connect the iPhone to your Mac. Open the Devices view in Xcode via Window > Devices and choose your device. Check that there is no error message shown here:

    Check Device

  5. Select your device and hit Run (⌘R):

    Select Iphone

    If you’re iPhone is not running the latest iOS version, it will be shown as Ineligible device:

    Ineligible Device

    To fix this, you can configure the Deployment Target to make your project compatible with older iOS versions:

    Deployment Target

    If you’re using this device the first time to run an app from Xcode, Xcode will offer to set up the device for development. It will ask for your Apple account password and automatically create a developer certificate and provisioning profile to sign app binaries for development and testing.

Example project

You can download the completed example project here: Beep.zip.

About the author

Ralf Ebert Ralf Ebert is an independent software developer and trainer for Mac OS X and iOS. He makes the Page Layers and Straight ahead apps and conducts iOS trainings and Git trainings in Germany.