iOS Swift Tutorial iOS9 Music App

Music Streaming App in Swift2

This month in the build an app every month series we will learn how to build a simple music streaming app. For the sake of simplicity we will use the itunes search api which provides the necessary information like sample music , images , artist Name, trackname etc.

Lets go ahead and start building the model for the MusicTrack and the MusicManager for downloading the neccessary information from the itunes api.

//  MusicTrack.swift
//  MusicApp
//
//  Created by Shrikar Archak on 6/17/15.
//  Copyright © 2015 Shrikar Archak. All rights reserved.
//

import Foundation

struct MusicTrack {
    var artistName: String
    var trackName : String
    var previewUrl: String
    var artworkUrl : String
    var thumbnail  : String
    init(data : NSDictionary){
        artistName = data["artistName"] as! String
        trackName = data["trackName"] as! String
        previewUrl = data["previewUrl"] as! String
        artworkUrl = (data["artworkUrl100"] as! String).stringByReplacingOccurrencesOfString("100x100", withString: "640x480")
        thumbnail =  data["artworkUrl100"] as! String
    }
}

After listening to the Building Better Apps with Value Types in Swift I thought it would be better to follow the best practices.

The next component is the MusicManager which will fetch the data and convert it into an array of MusicTrack and then call the onComplete callback to return the music tracks.  If are dealing with any networking you might face the issue of SSL handshake failure / NSURLDomainErr in iOS9 . Take a look on how to fix it NSUrlSession Changes in iOS9.

The MusicManager is pretty simple with just a single method to getMusicTracks . We will be using Alamofire for fetching the data.

//
//  MusicManager.swift
//  MusicApp
//
//  Created by Shrikar Archak on 6/17/15.
//  Copyright © 2015 Shrikar Archak. All rights reserved.
//

import Foundation
import Alamofire
class MusicManager {
    var musicTracks: [MusicTrack] = []
    let session = NSURLSession.sharedSession()
    func getMusicTracks(onComplete : (results :[MusicTrack]) -> Void) {
        
        Alamofire.request(.GET, URLString: "https://itunes.apple.com/search?term=one%20republic").responseJSON { (request, response, data , error) -> Void in
            print(error)
            if let tmpdata = data {
                for entry in tmpdata.valueForKey("results") as! [NSDictionary] {
                    let musicTrack = MusicTrack(data: entry)
                    self.musicTracks.append(musicTrack)
                }
                onComplete(results: self.musicTracks)
            }            
        }
    }

}

In the next part you will be learning more about building the UI using UITableViewController, UITableViewCell and UIStackViews. Stay tuned.

About the author

Shrikar

Backend/Infrastructure Engineer by Day. iOS Developer for the rest of the time.

  • Joshua Amersan

    Why do you use alamofire if you don’t use the built in serializer?

    • https://shrikar.com shrikar

      I agree I will update to use the serializers.

  • George Lee

    I highly support your efforts and am personally excited to learn from you.

/* ]]> */