0

I want to be able to change the background color of my app depending on the time of day. I believe the way to go about this is saying if the hour component is greater than whatever number, set the background to the nighttime background, otherwise it's the daytime background.

To test out what I was worried about, I threw

timeLabel.text = NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: .NoStyle, timeStyle: .FullStyle)

into viewDidLoad. This shows the time that the app loads. This also obviously keeps updating if I kill the app and reload it completely.

However, if the user goes the the home screen or goes to a different app, then comes back to this the time isn't going to be updated. It will show the time the app was first loaded up. Only if the user completely kills the app, which obviously can't be relied on, will the correct time be shown.

So in my example if my "switch to nighttime time" was 5pm, if the user loads up at the at at 4:30 and then goes to the homescreen, loads up the app at 5pm, nothing will be changed as the stored time will still be 4:30.

I tried throwing the code in viewDidAppear and nothing changed.

What code is run when the app is loaded up from being on the homescreen or coming back from another app?

gotnull
  • 26,454
  • 22
  • 137
  • 203
Barkley
  • 6,063
  • 3
  • 18
  • 25

3 Answers3

5

You want to key off of the UIApplicationDidBecomeActiveNotification event. Try this:

override func viewDidLoad() 
{
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(
        self,
        selector: "applicationDidBecomeActive:",
        name: UIApplicationDidBecomeActiveNotification,
        object: nil)
}

func applicationDidBecomeActive(notification: NSNotification) 
{
    // do something when the app is active again.
    timeLabel.text = NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: .NoStyle, timeStyle: .FullStyle)
}
wottle
  • 13,095
  • 4
  • 27
  • 68
  • Works perfectly. Thank you very much! I'm suprised didBecomeActive isn't covered in anything I've come across yet. Seems pretty important. – Barkley Jun 07 '16 at 01:34
  • Also, for more info around the different events (applicationWillEnterForeground vs. applicationDidBecomeActive), you can see this post for some great information: http://stackoverflow.com/a/9860393/3708242 – wottle Jun 07 '16 at 01:34
  • Wow yeah, just favorited that post for future reference. That's awesome. Thanks for your time. – Barkley Jun 07 '16 at 01:36
3

The method -applicationWillEnterForeground: in your application delegate will be called every time a user enters your app.

AdamPro13
  • 7,232
  • 2
  • 30
  • 28
2

You'll want to look at using NSCalendar for this:

let currentDate = NSDate() // You can input the custom as well 
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute, fromDate:  NSDate())
let currentHour = components.hour // You can play around with the ""components""

The following method is what you're after:

applicationDidBecomeActive: it's called every time a user opens the app.

gotnull
  • 26,454
  • 22
  • 137
  • 203
  • No right, maybe I wasn't quite clear. I understand how to pull what the user's current hour is. What I don't understand is how apps handle the difference between killing the app or just hitting the home button and coming back. In Xcode, hitting the 'stop square" simulates completely killing the app, so obviously every time I load up I get the updated time for when the app comes up because thats when viewdidload runs. But users don't completely kill apps every time they're done with them. whats the alternative to viewDidLoad for what I'm trying to accomplish? – Barkley Jun 07 '16 at 01:18
  • Yeah, thank you very much. Using the NSCalendar info you gave and the appdidbecomeActive are both exactly what I need. – Barkley Jun 07 '16 at 01:35