Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Weather App with Swift (Retired) Displaying Our Weather Data The End!

Brian Patterson
Brian Patterson
19,588 Points

AlertView.

I am trying to add an alert view in the application but this seems to be deprecated . How would you add an UIAlertViewController in the project?

Beatriz Macedo
Beatriz Macedo
9,903 Points

Hi Brian, I would add an alert message this way:

// Create the alert var alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert) // Add Back button to alert with no real action, just to close the alert dialog alert.addAction(UIAlertAction(title: "Back", style: UIAlertActionStyle.Default, handler: nil)) // Show the alert self.presentViewController(alert, animated: true, completion: nil)

Let me know if this works for you :D

5 Answers

Brian Patterson
Brian Patterson
19,588 Points

Where do I add the above code? Network Operation or the ViewController ?

Beatriz Macedo
Beatriz Macedo
9,903 Points

That depends, do you want this message to appear as a result of some action on the app, or as a result of a network operation? You should add it to where the action that makes the alert appear is.

Brian Patterson
Brian Patterson
19,588 Points

Can you have it in both and how would you implement it?

Brian Patterson
Brian Patterson
19,588 Points

If I put it in the Network Operation I get unresolved identifier errors. import Foundation

class NetworkOperation {

lazy var config: NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
lazy var session: NSURLSession = NSURLSession(configuration: self.config)
let queryURL: NSURL
typealias JSONDictionaryCompletion = ([String: AnyObject]? -> Void)

init(url: NSURL) {
    self.queryURL = url
}

func downloadJSONFromURL(completion: JSONDictionaryCompletion) {

    let request = NSURLRequest(URL: queryURL)
    let dataTask = session.dataTaskWithRequest(request) {
        (let data, let response, let error) in

        // 1. Check HTTP response for successful GET request
        if let httpResponse = response as? NSHTTPURLResponse {
            switch httpResponse.statusCode {
            case 200:
                // 2. Create JSON object with data
                let jsonDictionary = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as? [String: AnyObject]
                completion(jsonDictionary)
            default:
                println("GET request not successful. HTTP status code: \(httpResponse.statusCode)")
            }
        } else {
            println("Error: Not a valid HTTP response")
            // Create the alert 
            var alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
            // Add Back button to alert with no real action, just to close the alert dialog 
            alert.addAction(UIAlertAction(title: "Back", style: UIAlertActionStyle.Default, handler: nil)) // Show the alert 
            self.presentViewController(alert, animated: true, completion: nil)

        }
    }

    dataTask.resume()
}

}

Beatriz Macedo
Beatriz Macedo
9,903 Points

You should add an import to UIKit as UIAlertController is from UIKit. Just add this line of code to the top of your code below import Foundation:

import UIKit
Brian Patterson
Brian Patterson
19,588 Points

Thanks for that Beatriz. Although, I am running into a problem. The following line of code tells me that I have a " 'NetworkOperation' does not have a member named 'presentViewController'" Please see below.

self.presentViewController(alert, animated: true, completion: nil)

Brian Patterson
Brian Patterson
19,588 Points

Is this because I need to add this into the viewController? If so how?