Automatically update the interface when you open the application

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 ().

+7
swift
source share
2 answers

The standard trick to get the application to do something every time it opens is to store the date in NSUserDefault the first time you open the application, and then check the saved date for the current date when the application opens again. If enough time has passed, you do it, otherwise you will not do it. If you do this, you will save the current date in NSUserDefaults to start a new period.

+3
source share

1st decision. You can use notifications for this, in the viewcontroller add this to viewDidLoad :

 NSNotificationCenter.defaultCenter().addObserverForName(UIApplicationWillEnterForegroundNotification, object: nil, queue: NSOperationQueue.mainQueue()) { [unowned self] notification in getHumid() getTemp() } 

Second solution: alternatively in viewDidLoad :

  NSNotificationCenter.defaultCenter().addObserver(self, selector: "appWasOpened:", name: UIApplicationWillEnterForegroundNotification, object: nil) 

and new feature for viewcontroller

 func appWasOpened(notification: NSNotification!) { getHumid() getTemp() } 

in both cases, you need to override denit (this prevents observation from being called when the view manager is freed.

 deinit { NSNotificationCenter.defaultCenter().removeObserver(self) } 
+2
source share

All Articles