Coding Explorer Blog

Exploring how to code for iOS in Swift and Objective-C

  • Home
  • Apps
  • About
  • Contact

Updating an app when returning from background in iOS

Last updated on June 25, 2014

I am working on an app that needs to update the UI based on the current date when it is loaded.  I told it to do the update in my viewWillAppear, but about half the time it would not update when I loaded the program.  Shouldn’t viewWillAppear always be called when your view is about to appear?  Apparently not, but I’ll tell you how I got the functionality I was looking for.

Updating from the background with Notifications

So, viewWillAppear will run every time your view is loaded from disk, or being called to be onscreen by a segue from another view.  However, when I leave the app, use some other apps and come back, as far as my app was concerned, this view never left.  It left as far as the system was concerned, but in my app’s little world, it just had the same view up as it did when I left the app.  When I left the app, I sent it to the background.  That is the difference.

So, how do I update my screen when it returns from the background?  There are two notifications that would be of help here.  They are:

  • UIApplicationWillEnterForegroundNotification
  • UIApplicationDidBecomeActiveNotification

As your program is brought back from the background, these two notifications are raised, in that order.  UIApplicationWillEnterForegroundNotification will run right before the view should appear onscreen, and UIApplicationDidBecomeActiveNotification should run just after.  In my previous post about Supporting Dynamic Type, I mentioned how to subscribe to notifications, but for simplicity’s sake, I will show you here.

I added this to my viewWillAppear:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(updateGUI)
                                             name:UIApplicationWillEnterForegroundNotification
                                           object:[UIApplication sharedApplication]];

Then, in a new method updateGUI, just do whatever calculations or updates you need to do before the view comes back from the background.  In iOS7, the system takes a screenshot of your view when it is sent to the background, and shows that first when you load your app again, and then runs your code appropriately.  So even though we put this on the notification for before the view is visible, we still may see the previous value for a moment before the update is called.  I believe there are some ways to update the screenshot in the background every so often, but I do not know how yet.  I will look further in to it, but this still nonetheless fixed my original problem of only occasionally changing the value, and having to forcefully close the program and rerun it, or do some other action to call updateGUI.  I’m just taking this one step at a time.

Cleaning up NSNotification Center afterwards

And of course, we should clean up after ourselves, so again we will just unsubscribe from all notifications when we disappear.

-(void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

And that’s all there is to it.  I know this post is a bit shorter than usual, but I felt this was a useful tip to share.  I was confused about a problem I had, discovered how to fix it, and thought others might find the solution helpful.  If you have any questions, don’t hesitate to contact me on twitter @CodingExplorer, and I’ll see what I can do.

Thanks!

Filed Under: Syntax

Subscribe to the Coding Explorer Newsletter

* indicates required

Follow Us

Facebooktwitterrss

Recent Posts

  • Error Handling in Swift
  • Creating and Modifying a URL in your Swift App
  • Watch Connectivity in Swift — Application Context
  • watchOS Hello World App in Swift
  • API Availability Checking in Swift

Categories

  • Class Reference
  • General
  • Interface Builder
  • My Apps
  • Objective-C
  • Swift
  • Syntax
  • Tutorial
  • Uncategorized

Archives

  • May 2016
  • March 2016
  • February 2016
  • December 2015
  • July 2015
  • June 2015
  • April 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • January 2014
  • November 2013
  • September 2013
  • August 2013
  • July 2013
  • Terms Of Use
  • Privacy Policy
  • Affiliate Disclaimer

Subscribe to the Coding Explorer Newsletter

* indicates required

Copyright © 2025 Wayne Media LLC · Powered by Genesis Framework · WordPress