I successfully managed to get my weather application to work; display of current test data, etc. But now the only problem is that I can only get the application to receive data by clicking the "Update" button that I did. How can I configure the application to update the user interface when it opens?
Here is my working code:
import Foundation import UIKit class ViewController: UIViewController { @IBOutlet weak var tempLabel: UILabel! @IBOutlet weak var humidLabel: UILabel! @IBOutlet weak var refresh: UIButton! @IBOutlet weak var negitiveSymbol: UILabel! func getTemp(){ var urlString = "http://torsher.ca/Weather/temperature.txt" var url = NSURL(string: urlString) let task = NSURLSession.sharedSession().dataTaskWithRequest(NSURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 15.0)){(data, response, error) in var urlContent = (NSString(data: data, encoding: NSUTF8StringEncoding)) var tempContent:String = urlContent as String println(tempContent) dispatch_async(dispatch_get_main_queue()) { self.tempLabel.text = (tempContent) } } task.resume() } func getHumid(){ var urlString = "http://torsher.ca/Weather/humidity.txt" var url = NSURL(string: urlString) let task = NSURLSession.sharedSession().dataTaskWithRequest(NSURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 15.0)){(data, response, error) in var urlContent = (NSString(data: data, encoding: NSUTF8StringEncoding)) var humidContent:String = urlContent as String println(humidContent) dispatch_async(dispatch_get_main_queue()) { self.humidLabel.text = (humidContent) } } task.resume() } @IBAction func refreshPressed(sender: AnyObject, forEvent event: UIEvent) { getTemp() } @IBAction func refreshPress(sender: AnyObject, forEvent event: UIEvent) { getHumid() } override func viewDidLoad() { super.viewDidLoad() } }
What I was looking at is talking about using the AppDelegate.swift file and the func applicationDidEnterBackground built-in function (application: UIApplication), but everything I'm trying to either break the application or just doesn't work. I want the application to run the getTemp function, the getHumid function, and then update the user interface with the values that I did in the IBAction functions using buttons.
Any help is greatly appreciated.
EDIT:
// // ViewController.swift // WeatherStation // // Created by on 2015-04-16. // Copyright (c) 2015 . All rights reserved. // import Foundation import UIKit class ViewController: UIViewController { @IBOutlet weak var tempLabel: UILabel! @IBOutlet weak var humidLabel: UILabel! @IBOutlet weak var refresh: UIButton! @IBOutlet weak var negitiveSymbol: UILabel! @IBOutlet weak var dateUpdate: UILabel! func getTemp() //Gets the temeprature from the server and displays it. { var urlString = "http://torsher.ca/Weather/temperature.txt" var url = NSURL(string: urlString) let task = NSURLSession.sharedSession().dataTaskWithRequest(NSURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 15.0)){(data, response, error) in var urlContent = (NSString(data: data, encoding: NSUTF8StringEncoding)) var tempContent:String = urlContent as String println(tempContent) dispatch_async(dispatch_get_main_queue()) { self.tempLabel.text = (tempContent) } } task.resume() } func getHumid() //Gets the humidity from the server and displays it. { var urlString = "http://torsher.ca/Weather/humidity.txt" var url = NSURL(string: urlString) let task = NSURLSession.sharedSession().dataTaskWithRequest(NSURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 15.0)){(data, response, error) in var urlContent = (NSString(data: data, encoding: NSUTF8StringEncoding)) var humidContent:String = urlContent as String println(humidContent) dispatch_async(dispatch_get_main_queue()) { self.humidLabel.text = (humidContent) } } task.resume() } @IBAction func refreshPressed(sender: AnyObject, forEvent event: UIEvent) //Updates UI with current temperature when refresh is pressed. { getTemp() } @IBAction func refreshPress(sender: AnyObject, forEvent event: UIEvent) //Updates UI with current humidity when refresh is pressed. { getHumid() } func appWasOpened(notification: NSNotification!) { getHumid() getTemp() } deinit { NSNotificationCenter.defaultCenter().removeObserver(foregroundNotification) } override func viewDidLoad() { super.viewDidLoad() NSNotificationCenter.defaultCenter().addObserver(self, selector: "appWasOpened:", name: UIApplicationWillEnterForegroundNotification, object: nil) } }
Trying to add a second solution could not work with deinit ().