Skip to content

Commit caf4d07

Browse files
committed
Part 3
1 parent 41763f4 commit caf4d07

File tree

6 files changed

+129
-99
lines changed

6 files changed

+129
-99
lines changed

MusicPlayer.xcodeproj/project.pbxproj

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88

99
/* Begin PBXBuildFile section */
1010
0270480919C8E08B00FDA1C5 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0270480819C8E08B00FDA1C5 /* AppDelegate.swift */; };
11-
0270480B19C8E08B00FDA1C5 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0270480A19C8E08B00FDA1C5 /* ViewController.swift */; };
11+
0270480B19C8E08B00FDA1C5 /* SearchResultsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0270480A19C8E08B00FDA1C5 /* SearchResultsViewController.swift */; };
1212
0270480E19C8E08B00FDA1C5 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 0270480C19C8E08B00FDA1C5 /* Main.storyboard */; };
1313
0270481019C8E08B00FDA1C5 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0270480F19C8E08B00FDA1C5 /* Images.xcassets */; };
1414
0270481319C8E08B00FDA1C5 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 0270481119C8E08B00FDA1C5 /* LaunchScreen.xib */; };
1515
0270481F19C8E08B00FDA1C5 /* MusicPlayerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0270481E19C8E08B00FDA1C5 /* MusicPlayerTests.swift */; };
16+
0270482919C8E2E700FDA1C5 /* APIController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0270482819C8E2E700FDA1C5 /* APIController.swift */; };
1617
/* End PBXBuildFile section */
1718

1819
/* Begin PBXContainerItemProxy section */
@@ -29,13 +30,14 @@
2930
0270480319C8E08B00FDA1C5 /* MusicPlayer.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MusicPlayer.app; sourceTree = BUILT_PRODUCTS_DIR; };
3031
0270480719C8E08B00FDA1C5 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3132
0270480819C8E08B00FDA1C5 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
32-
0270480A19C8E08B00FDA1C5 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
33+
0270480A19C8E08B00FDA1C5 /* SearchResultsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchResultsViewController.swift; sourceTree = "<group>"; };
3334
0270480D19C8E08B00FDA1C5 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
3435
0270480F19C8E08B00FDA1C5 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
3536
0270481219C8E08B00FDA1C5 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = "<group>"; };
3637
0270481819C8E08B00FDA1C5 /* MusicPlayerTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = MusicPlayerTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
3738
0270481D19C8E08B00FDA1C5 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3839
0270481E19C8E08B00FDA1C5 /* MusicPlayerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MusicPlayerTests.swift; sourceTree = "<group>"; };
40+
0270482819C8E2E700FDA1C5 /* APIController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APIController.swift; sourceTree = "<group>"; };
3941
/* End PBXFileReference section */
4042

4143
/* Begin PBXFrameworksBuildPhase section */
@@ -78,11 +80,12 @@
7880
isa = PBXGroup;
7981
children = (
8082
0270480819C8E08B00FDA1C5 /* AppDelegate.swift */,
81-
0270480A19C8E08B00FDA1C5 /* ViewController.swift */,
83+
0270480A19C8E08B00FDA1C5 /* SearchResultsViewController.swift */,
8284
0270480C19C8E08B00FDA1C5 /* Main.storyboard */,
8385
0270480F19C8E08B00FDA1C5 /* Images.xcassets */,
8486
0270481119C8E08B00FDA1C5 /* LaunchScreen.xib */,
8587
0270480619C8E08B00FDA1C5 /* Supporting Files */,
88+
0270482819C8E2E700FDA1C5 /* APIController.swift */,
8689
);
8790
path = MusicPlayer;
8891
sourceTree = "<group>";
@@ -212,8 +215,9 @@
212215
isa = PBXSourcesBuildPhase;
213216
buildActionMask = 2147483647;
214217
files = (
215-
0270480B19C8E08B00FDA1C5 /* ViewController.swift in Sources */,
218+
0270480B19C8E08B00FDA1C5 /* SearchResultsViewController.swift in Sources */,
216219
0270480919C8E08B00FDA1C5 /* AppDelegate.swift in Sources */,
220+
0270482919C8E2E700FDA1C5 /* APIController.swift in Sources */,
217221
);
218222
runOnlyForDeploymentPostprocessing = 0;
219223
};

MusicPlayer/APIController.swift

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// APIController.swift
3+
// MusicPlayer
4+
//
5+
// Created by Jameson Quave on 9/16/14.
6+
// Copyright (c) 2014 JQ Software LLC. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
protocol APIControllerProtocol {
12+
func didReceiveAPIResults(results: NSDictionary)
13+
}
14+
15+
class APIController {
16+
17+
var delegate: APIControllerProtocol?
18+
19+
init() {
20+
}
21+
22+
func searchItunesFor(searchTerm: String) {
23+
24+
// The iTunes API wants multiple terms separated by + symbols, so replace spaces with + signs
25+
let itunesSearchTerm = searchTerm.stringByReplacingOccurrencesOfString(" ", withString: "+", options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil)
26+
27+
// Now escape anything else that isn't URL-friendly
28+
if let escapedSearchTerm = itunesSearchTerm.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) {
29+
let urlPath = "http://itunes.apple.com/search?term=\(escapedSearchTerm)&media=software"
30+
let url: NSURL = NSURL(string: urlPath)
31+
let session = NSURLSession.sharedSession()
32+
let task = session.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in
33+
println("Task completed")
34+
if(error != nil) {
35+
// If there is an error in the web request, print it to the console
36+
println(error.localizedDescription)
37+
}
38+
var err: NSError?
39+
40+
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
41+
if(err != nil) {
42+
// If there is an error parsing JSON, print it to the console
43+
println("JSON Error \(err!.localizedDescription)")
44+
}
45+
let results: NSArray = jsonResult["results"] as NSArray
46+
self.delegate?.didReceiveAPIResults(jsonResult)
47+
})
48+
49+
task.resume()
50+
}
51+
}
52+
53+
}

MusicPlayer/Base.lproj/LaunchScreen.xib

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6214" systemVersion="14A314h" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6245" systemVersion="13E28" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES">
33
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6207"/>
4+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6238"/>
55
<capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/>
66
</dependencies>
77
<objects>

MusicPlayer/Base.lproj/Main.storyboard

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6238"/>
55
</dependencies>
66
<scenes>
7-
<!--View Controller-->
7+
<!--Search Results View Controller-->
88
<scene sceneID="ufC-wZ-h7g">
99
<objects>
10-
<viewController id="vXZ-lx-hvc" customClass="ViewController" customModule="MusicPlayer" customModuleProvider="target" sceneMemberID="viewController">
10+
<viewController id="vXZ-lx-hvc" customClass="SearchResultsViewController" customModule="MusicPlayer" customModuleProvider="target" sceneMemberID="viewController">
1111
<layoutGuides>
1212
<viewControllerLayoutGuide type="top" id="jyV-Pf-zRb"/>
1313
<viewControllerLayoutGuide type="bottom" id="2fi-mo-0CV"/>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//
2+
// ViewController.swift
3+
// MusicPlayer
4+
//
5+
// Created by Jameson Quave on 9/16/14.
6+
// Copyright (c) 2014 JQ Software LLC. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
class SearchResultsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, APIControllerProtocol {
12+
13+
@IBOutlet var appsTableView : UITableView?
14+
var tableData = []
15+
var api = APIController()
16+
17+
override func viewDidLoad() {
18+
super.viewDidLoad()
19+
api.searchItunesFor("Angry Birds")
20+
api.delegate = self
21+
}
22+
23+
override func didReceiveMemoryWarning() {
24+
super.didReceiveMemoryWarning()
25+
// Dispose of any resources that can be recreated.
26+
}
27+
28+
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
29+
return tableData.count
30+
}
31+
32+
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
33+
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
34+
35+
let rowData: NSDictionary = self.tableData[indexPath.row] as NSDictionary
36+
37+
cell.textLabel?.text = rowData["trackName"] as? String
38+
39+
// Grab the artworkUrl60 key to get an image URL for the app's thumbnail
40+
let urlString: NSString = rowData["artworkUrl60"] as NSString
41+
let imgURL: NSURL = NSURL(string: urlString)
42+
43+
// Download an NSData representation of the image at the URL
44+
let imgData: NSData = NSData(contentsOfURL: imgURL)
45+
cell.imageView?.image = UIImage(data: imgData)
46+
47+
// Get the formatted price string for display in the subtitle
48+
let formattedPrice: NSString = rowData["formattedPrice"] as NSString
49+
50+
cell.detailTextLabel?.text = formattedPrice
51+
52+
return cell
53+
}
54+
55+
func didReceiveAPIResults(results: NSDictionary) {
56+
var resultsArr: NSArray = results["results"] as NSArray
57+
dispatch_async(dispatch_get_main_queue(), {
58+
self.tableData = resultsArr
59+
self.appsTableView!.reloadData()
60+
})
61+
}
62+
63+
}
64+

MusicPlayer/ViewController.swift

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)