Boon
Boon

Reputation: 41510

How to react to applicationWillResignActive from anywhere?

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?

Upvotes: 34

Views: 23673

Answers (4)

James Van Boxtel
James Van Boxtel

Reputation: 2415

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];
}

Upvotes: 75

Ed Marty
Ed Marty

Reputation: 39700

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.

Upvotes: 12

Jane Sales
Jane Sales

Reputation: 13546

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.

Upvotes: 4

Andrew Grant
Andrew Grant

Reputation: 58804

Your topic and question are asking slightly different things.

Your application will receive applicationWillResignActive, along with applicationWillTerminate, automatically. No subscription is necessary, just implement the function in your app.

As to how to respond, this is down to the application. While you can choose to do nothing the recommended behavior is that you cease or slow any non-critical functionality. E.g. if you were a game you would stop updating the display and/or pause the game.

Upvotes: 0

Related Questions