Skip to content

Commit 5c8e254

Browse files
author
antnasce
committed
Adding simple error alert for when network connection is down
Would be nice to add a refresh button/pull to refresh and a search bar for searching other artists.
1 parent 3b107b7 commit 5c8e254

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

MusicPlayer.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
0270482F19C8F43500FDA1C5 /* DetailsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0270482E19C8F43500FDA1C5 /* DetailsViewController.swift */; };
2020
0270483119C8F6D000FDA1C5 /* Track.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0270483019C8F6D000FDA1C5 /* Track.swift */; };
2121
0270483519C8F80000FDA1C5 /* TrackCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0270483419C8F80000FDA1C5 /* TrackCell.swift */; };
22+
ED63ECE81C21694A001ACFC3 /* ErrorAlerter.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED63ECE71C21694A001ACFC3 /* ErrorAlerter.swift */; };
2223
/* End PBXBuildFile section */
2324

2425
/* Begin PBXContainerItemProxy section */
@@ -48,6 +49,7 @@
4849
0270482E19C8F43500FDA1C5 /* DetailsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DetailsViewController.swift; sourceTree = "<group>"; };
4950
0270483019C8F6D000FDA1C5 /* Track.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Track.swift; sourceTree = "<group>"; };
5051
0270483419C8F80000FDA1C5 /* TrackCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TrackCell.swift; sourceTree = "<group>"; };
52+
ED63ECE71C21694A001ACFC3 /* ErrorAlerter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ErrorAlerter.swift; sourceTree = "<group>"; };
5153
/* End PBXFileReference section */
5254

5355
/* Begin PBXFrameworksBuildPhase section */
@@ -92,6 +94,7 @@
9294
0270482A19C8EC3000FDA1C5 /* Blank52.png */,
9395
0270480819C8E08B00FDA1C5 /* AppDelegate.swift */,
9496
0270480A19C8E08B00FDA1C5 /* SearchResultsViewController.swift */,
97+
ED63ECE71C21694A001ACFC3 /* ErrorAlerter.swift */,
9598
0270480C19C8E08B00FDA1C5 /* Main.storyboard */,
9699
0270480F19C8E08B00FDA1C5 /* Images.xcassets */,
97100
0270481119C8E08B00FDA1C5 /* LaunchScreen.xib */,
@@ -237,6 +240,7 @@
237240
0270480919C8E08B00FDA1C5 /* AppDelegate.swift in Sources */,
238241
0270482919C8E2E700FDA1C5 /* APIController.swift in Sources */,
239242
0270482D19C8F35700FDA1C5 /* Album.swift in Sources */,
243+
ED63ECE81C21694A001ACFC3 /* ErrorAlerter.swift in Sources */,
240244
0270482F19C8F43500FDA1C5 /* DetailsViewController.swift in Sources */,
241245
0270483119C8F6D000FDA1C5 /* Track.swift in Sources */,
242246
);

MusicPlayer/APIController.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,15 @@ class APIController {
2828
if(error != nil) {
2929
// If there is an error in the web request, print it to the console
3030
print(error!.localizedDescription)
31+
32+
showErrorAlert(error!.localizedDescription)
33+
34+
// Bail here because we can't process the data any further
35+
return
3136
}
3237

3338
do {
3439
let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
35-
//let results: NSArray = jsonResult["results"] as! NSArray
3640
self.delegate.didReceiveAPIResults(jsonResult) // THIS IS THE NEW LINE!!
3741
} catch {
3842
// If there is an error parsing JSON, print it to the console

MusicPlayer/ErrorAlerter.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// ErrorHandler.swift
3+
// MusicPlayer
4+
//
5+
// Created by Ant on 16/12/2015.
6+
//
7+
8+
import Foundation
9+
import UIKit
10+
11+
func showErrorAlert(message: String) {
12+
13+
dispatch_async(dispatch_get_main_queue()) {
14+
let alert = UIAlertView()
15+
alert.title = "Hmm, something's not right..."
16+
alert.message = message
17+
alert.addButtonWithTitle("Got it!")
18+
alert.show()
19+
}
20+
}

MusicPlayer/SearchResultsViewController.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ class SearchResultsViewController: UIViewController, UITableViewDataSource, UITa
5050
let urlString = album.thumbnailImageURL
5151

5252
// Check our image cache for the existing key. This is just a dictionary of UIImages
53-
//var image: UIImage? = self.imageCache.valueForKey(urlString) as? UIImage
54-
var image = self.imageCache[urlString] as UIImage!
53+
var image = self.imageCache[urlString]
5554

5655
if( image == nil ) {
5756
// If the image does not exist, we need to download it

0 commit comments

Comments
 (0)