Beginner Tutorial iOS & Swift: How to develop an iPhone app

This tutorial will show you how to:

Last update: July 16, 2018 | Tested with: Xcode 9.4

Requirements

To develop iOS apps for the iPhone/iPad using Xcode 9, you’ll need a Mac running at least macOS 10.13 High Sierra.

Install Xcode and create a new iOS project

  1. Open the App Store app and install the latest Xcode:

    Install Xcode
  2. Run Xcode and create a new project with File » New » Project ⌘⇧N. Select iOS » Application » Single View Application:

    File » New » Project » iOS » Application » Single View Application
  3. 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.
    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: choose System as type to make the button tappable/tinted blue, remove the title and select the Trumpet image:

Create Layout Constraints to center the Button

  1. Create Layout Constraints to center the button horizontally and vertically using the Align button:

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 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, scroll down 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

Btn download 3c20f11b8e Download the finished example project
Btn read 3c0e607615 Continue reading: UITableViewController - iOS & Swift Tutorial
Btn read 3c0e607615 iOS Developer Blog
Btn subscribe 930758687e Subscribe: Email · Twitter
Btn training bbbdf557d2 Next iOS training: 19. - 23. November 2018, Hamburg
Btn about 5378472193 About me · Contact