iOS & Swift 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 8.2, you’ll need a Mac running the latest Mac OS X 10.11 El Capitan.

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 8.2):

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

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

    File » New » Project » 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 center of 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 beep:

    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 beep 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:

    import UIKit
    import AVFoundation
    
    class ViewController: UIViewController {
    
        let player = AVPlayer(url: Bundle.main.url(forResource: "trumpet", withExtension: "mp3")!)
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        @IBAction func beep(_ sender: Any) {
            player.play()
        }

}

Running the app in the Simulator

  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

Example code

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

Next

Next tutorial: Creating tables using UITableViewController

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 in Germany since 2009.