Search code examples
iosobjective-csleepbackground-processexit

Detect when app finishes loading after opening from background


When my app is first opened there is a long loading time so I can display a loading screen. When the user exits the app by clicking the home button then re-opens it (the viewDidLoad/viewDidAppear methods aren't called again) the app has another loading period, I guess while it's "waking up".

What method can I use to detect the user hitting the home button to send the app into the background and what method can I use to detect that the app has been revived from the background?

This should be sufficient enough to provide my loading screen properly but just in-case. Is there also a method for detecting when the loading has finished after a "revival"?


Solution

  • You can register a notification for UIApplicationWillEnterForegroundNotification. There you can do your stuff.

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    
    // Register for the notifcation 
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(refreshView) name:UIApplicationWillEnterForegroundNotification object:nil];  
    
    }  
    
    -(void)refreshView
    {
    /* 
     Invoked when application enters foreground. Do your stuff
    
     */   
    
     }  
    

    To remove observer

    -(void)dealloc
    {
      [[NSNotificationCenter defaultCenter] removeObserver:self];
      [super  dealloc];
    }