Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-29-2010, 11:12 AM   #1 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 127
Error404 is on a distinguished road
Default applicationDidEnterBackground only works in appdelegate class

Hi,

When the program goes into background mode, I want to do do a little something inside some of my classes... However, when i put the:

- (void) applicationDidEnterBackgroundUIApplication *)application { }

inside my class, It doesn't get called... Only when its inside the main appdelegate class..

How do you make it work inside any class?

Thanks
__________________
http://www.innovationdreams.se
Error404 is offline   Reply With Quote
Old 08-29-2010, 08:51 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Error404 View Post
Hi,

When the program goes into background mode, I want to do do a little something inside some of my classes... However, when i put the:

- (void) applicationDidEnterBackgroundUIApplication *)application { }

inside my class, It doesn't get called... Only when its inside the main appdelegate class..

How do you make it work inside any class?

Thanks

You don't. applicationDidEnterBackground is an app delegate method. The UIApplication object sends this message to the app delegate, and ONLY to the app delegate.

You could have your app delegate broadcast an NSNotification, and then any of your objects could listen for that notification. Something like this:

Code:
- (void) applicationDidEnterBackground:(UIApplication *)application 
{   
  [[NSNotificationCenter defaultCenter] postNotificationName: @"didEnterBackground" 
    object: nil 
    userInfo: nil];
}
And in any object that you want to get, in it's init method:

Code:
  [[NSNotificationCenter defaultCenter] addObserver: self 
    selector: @selector(enteredBackground:) 
    name: @"didEnterBackground" 
    object: nil];
That will cause the object's enteredBackground: method to be called when the app enters the background. (It will crash if the object doesn't have a enteredBackground: method)
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 08-30-2010, 05:55 AM   #3 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 127
Error404 is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
You don't. applicationDidEnterBackground is an app delegate method. The UIApplication object sends this message to the app delegate, and ONLY to the app delegate.

You could have your app delegate broadcast an NSNotification, and then any of your objects could listen for that notification. Something like this:

Code:
- (void) applicationDidEnterBackground:(UIApplication *)application 
{   
  [[NSNotificationCenter defaultCenter] postNotificationName: @"didEnterBackground" 
    object: nil 
    userInfo: nil];
}
And in any object that you want to get, in it's init method:

Code:
  [[NSNotificationCenter defaultCenter] addObserver: self 
    selector: @selector(enteredBackground:) 
    name: @"didEnterBackground" 
    object: nil];
That will cause the object's enteredBackground: method to be called when the app enters the background. (It will crash if the object doesn't have a enteredBackground: method)

Amazing! I dont know why, but I never used notifications before.. it will, beside fixing this challenge, help me a lot in the future.

Thanks a lot

Cheers
__________________
http://www.innovationdreams.se
Error404 is offline   Reply With Quote
Old 09-13-2010, 07:37 PM   #4 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 12
Mixsiah is on a distinguished road
Default

[quote=Duncan C;239328]You don't. applicationDidEnterBackground is an app delegate method. The UIApplication object sends this message to the app delegate, and ONLY to the app delegate.

You could have your app delegate broadcast an NSNotification, and then any of your objects could listen for that notification. Something like this:

Code:
- (void) applicationDidEnterBackground:(UIApplication *)application 
{   
  [[NSNotificationCenter defaultCenter] postNotificationName: @"didEnterBackground" 
    object: nil 
    userInfo: nil];
}
And in any object that you want to get, in it's init method:

Code:
  [[NSNotificationCenter defaultCenter] addObserver: self 
    selector: @selector(enteredBackground:) 
    name: @"didEnterBackground" 
    object: nil];

------
Where do you place this code? in a method that I want to call?

Example I want the reloadData method to be called via the NSTimer, where would I place this NSNotification code?
Mixsiah is offline   Reply With Quote
Old 09-13-2010, 07:52 PM   #5 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

[quote=Mixsiah;245128]
Quote:
Originally Posted by Duncan C View Post
You don't. applicationDidEnterBackground is an app delegate method. The UIApplication object sends this message to the app delegate, and ONLY to the app delegate.

You could have your app delegate broadcast an NSNotification, and then any of your objects could listen for that notification. Something like this:

Code:
- (void) applicationDidEnterBackground:(UIApplication *)application 
{   
  [[NSNotificationCenter defaultCenter] postNotificationName: @"didEnterBackground" 
    object: nil 
    userInfo: nil];
}
And in any object that you want to get, in it's init method:

Code:
  [[NSNotificationCenter defaultCenter] addObserver: self 
    selector: @selector(enteredBackground:) 
    name: @"didEnterBackground" 
    object: nil];

------
Where do you place this code? in a method that I want to call?

Example I want the reloadData method to be called via the NSTimer, where would I place this NSNotification code?
Notifications and timers are two different things. The original poster was asking for a way for objects other than the app delegate to be notified when the app enters the background.

If you have questions about using timers you should start a new thread.

If you want other objects to be notified about applicationDidEnterBackground messages, the description above tells you exactly where to put it. The applicationDidEnterBackground method goes in the app delegate.

If you have another object, like a view controller, that you want to get a notice when the app gets a did enter background, you add 2 things to the object that you want to be notified:

1. Put the addObserver call (above) in that object's init method.

2. You then need to write an enteredBackground: method method with the following signature:

- (void) enteredBackground: (NSNotification*) notification;


That's it.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 09-13-2010, 08:10 PM   #6 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 12
Mixsiah is on a distinguished road
Default

[quote=Duncan C;245133]
Quote:
Originally Posted by Mixsiah View Post

Notifications and timers are two different things. The original poster was asking for a way for objects other than the app delegate to be notified when the app enters the background.

If you have questions about using timers you should start a new thread.

If you want other objects to be notified about applicationDidEnterBackground messages, the description above tells you exactly where to put it. The applicationDidEnterBackground method goes in the app delegate.

If you have another object, like a view controller, that you want to get a notice when the app gets a did enter background, you add 2 things to the object that you want to be notified:

1. Put the addObserver call (above) in that object's init method.

2. You then need to write an enteredBackground: method method with the following signature:

- (void) enteredBackground: (NSNotification*) notification;


That's it.
Hi Duncan

I am a newbie so please bare with me...


Yes the timer works perfectly, I now just need to keep the app running in the background.

I dont know where the init method is? Can you please tell me how to do this? and where to put it?


Yes I have a viewcontroller which is where all the methods execute like NSTimer, refresh data etc...
Mixsiah is offline   Reply With Quote
Old 09-13-2010, 09:14 PM   #7 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 12
Mixsiah is on a distinguished road
Default

[quote=Duncan C;245133]
Quote:
Originally Posted by Mixsiah View Post

Notifications and timers are two different things. The original poster was asking for a way for objects other than the app delegate to be notified when the app enters the background.

If you have questions about using timers you should start a new thread.

If you want other objects to be notified about applicationDidEnterBackground messages, the description above tells you exactly where to put it. The applicationDidEnterBackground method goes in the app delegate.

If you have another object, like a view controller, that you want to get a notice when the app gets a did enter background, you add 2 things to the object that you want to be notified:

1. Put the addObserver call (above) in that object's init method.

2. You then need to write an enteredBackground: method method with the following signature:

- (void) enteredBackground: (NSNotification*) notification;


That's it.

Does that code go into the main.m ?

int main(int argc, char *argv[]) {

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;

//Here is the inserted code?

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enteredBackground) name: @"didEnterBackground" object: nil];

}
Mixsiah is offline   Reply With Quote
Old 09-13-2010, 11:43 PM   #8 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

There's a set of notifications that automatically gets sent by the system, so you don't have to send your own.

UIApplication Class Reference

Just sign up for UIApplicationDidEnterBackgroundNotification from [UIApplication sharedApplication]. I didn't compile, but this should work:

Code:
  [[NSNotificationCenter defaultCenter] addObserver: whatever
    selector: @selector(enteredBackground:) 
    name: UIApplicationDidEnterBackgroundNotification
    object: nil];
__________________

Free Games!
smasher is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 346
6 members and 340 guests
dre, freewind, hain, HemiMG, lendo, Newbie123
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,894
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:59 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0