Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to react to applicationWillResignActive from anywhere?

Tags:

iphone

What's the code to subscribe to an event like applicationWillResignActive in any place in your iphone application?

[UPDATE]

Let me rephrase my question. I don't want to respond to this in my application delegate, but rather listen to this event from another class. Is that possible or I need to pass the event from the application delegate to the concerning class?

like image 267
Boon Avatar asked Feb 26 '09 08:02

Boon


3 Answers

Looks like you are looking for this code.

- (void) applicationWillResign {
    NSLog(@"About to lose focus");
}

- (void) myMethod { 
    [[NSNotificationCenter defaultCenter]
        addObserver:self
        selector:@selector(applicationWillResign)
        name:UIApplicationWillResignActiveNotification 
        object:NULL];
}
like image 56
James Van Boxtel Avatar answered Nov 01 '22 11:11

James Van Boxtel


Take a look at the documentation for the method you're talking about:

applicationWillResignActive:

Tells the delegate that the application will become inactive. This method is optional.

- (void)applicationWillResignActive:(UIApplication *)application

[...]

Discussion

[...]

Just before it becomes inactive, the application also posts a UIApplicationWillResignActiveNotification.

like image 29
Ed Marty Avatar answered Nov 01 '22 12:11

Ed Marty


Implement the method below in your application delegate:

-(void)applicationWillResignActive:(UIApplication *)application

This allows you to react when the application becomes inactive - when this is the case, it is executing but not dispatching incoming events. This happens, for example, when an overlay window pops up or when the device is locked.

Just before it becomes inactive, the application also posts a UIApplicationWillResignActiveNotification.

like image 4
Jane Sales Avatar answered Nov 01 '22 13:11

Jane Sales