My first iPhone App

After carrying around my iPhone 6 for two and a half years I finally wanted to know how to build an iOS app.

Getting started

I used this tutorial from apple and rolled with the punches:

Installing XCode

takes ages!  Download 4,6 GB 🙁

First issue: when accidentally making the wrong connection between a UI-Element and the ViewController (@IBOutlet instead of @IBAction) You have to remove the connection in the code _and_ in the storyboard via context menu.

First own app

After I was done with the tutorial I wrote an app I always wanted to write:

A music player app to make learning songs easy. Use case: You want to learn a solo from e.g. Metallica’s “Nothing else matters” and You want to play along the music.

Requirements so far:

  • Browse and play songs from music library
  • Set marker at the beginning of a section you want to practice
  • Possibility to rename the button to a meaningful name e.g. Solo, Chorus
  • Select play time via a scrub slider

Access media library on device

To access the music library the user has to acknowledge the access.  An app that uses the music library has to add NSAppleMusicUsageDescription to the plist file. To get all the items from your music library is pretty straight forward then:

import MediaPlayer

let mediaItems = MPMediaQuery.songs().items

Here You can see the first view, a table list of all songs:

Add an audioplayer

is pretty straight forward:

do {
    myAudioPlayer = try AVAudioPlayer(contentsOf: url!)
    myAudioPlayer.prepareToPlay()
    myAudioPlayer.volume = 1.0
} catch  {
    print(error)
}

Add play and pause button

[x] add slider for selecting track time

[x] round the corners of a button

button.layer.cornerRadius = 5

[x] add a popup view to show when marker is set

[x] Convert TimeInterval to hours, mins seconds

func stringFromTimeInterval(interval: TimeInterval) -> String {
    let ti = NSInteger(interval)
    
    let tenth = Int((interval.truncatingRemainder(dividingBy: 1.0)) * 1000) / 100
    let seconds = ti % 60
    let minutes = (ti / 60) % 60
    let hours = (ti / 3600)
    
    return String(format: "%0.1d:%0.2d:%0.2d.%0.1d", hours, minutes, seconds, tenth)
}

Deploying the app to your own device

Although the xcode simulator is nice you cannot access music from within the simulator so getting the app to work on a real device is mandatory early on.

There is the myth that you have to be a paying member of the apple developer program to test your app on your own phone. That’s wrong!

Anyway you need an account if You want to publish the app in the app store.

  1. Open Xode preferences (Xcode > Preferences…)
  2. Click the ‘Accounts’ tab
  3. Login with your Apple ID (+ > Add Apple ID…)
  4. Project Editor -> Signing -> Team -> <User>
  5. On iPhone: Settings -> General -> Device Management -> Trust Developer

After that You can deploy and test the app on your phone or tablet.

Beta-Test of the App

After I’ve implemented the basic use cases I showed the app to some colleagues. And got a bunch of new requirements and tips for improvements.

  • Instead to the slider show a waveform to make identifying the section easier
  • Set marker for start and end to replay the loop
  • Persist already set markers
  • Mark tracks which already have markers
  • Start and stop playing via Siri

My colleagues found a couple of bugs as well:

  • when selecting a track which is not on your device at the moment, the app crashes
  • the layout is not suitable for landscape orientation

Stay tuned for the second part!