views:

29

answers:

1

Hello,

I'm trying to unload a view controller from view when the iPhone goes to sleep. My app has a stopwatch that has to keep counting when the phone goes to sleep or call comes in or the user closes the app without logging out.

I have all this functionality in place, I'm capturing all start times and stop times and upon re-entering the stopwatch view controller, I calculate the difference. It all works beautifully. When I was doing some additional testing I realised I hadn't catered for the iPhone going into sleep mode.

So all I need to do to make sure my stopwatch is correct bring the user back to the app home screen. I know the following method is called when the app goes to sleep:

-(void)applicationWillResignActive:(UIApplication *)application

How do I unload the stopwatch view controller from my app delegate ?

---- UPDATE ----

kpower, thanks for your feedback. I've implemented the following code:

In my App Delegate:

- (void)applicationWillResignActive:(UIApplication *)application
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"AppIsAsleep" object:nil];
}

In my view controller, I have the following:

-(void)viewDidLoad 
{   
    // Add Observer.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewDidUnload:) name:@"AppIsAsleep" object:nil];
}

- (void)viewDidUnload {
    //Remove the Observer.
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"AppIsAsleep" object:nil];
}

When the phone goes to sleep, it actually closes the app, am I doing something wrong ?

Regards, Stephen

+1  A: 
kpower
kpower, thanks for your feedback. I've updated my question above with the code I've implemented. But it seems to be shutting the app down completely. Any thoughts ?
Stephen