I've tried this iOS Sprite Kit tutorial and have created a similar app. However, I notice that when I press the home button to go to iOS home screen, I get a bad access exception in xCode. When I go back into the app, it starts from the beginning.
How can I properly close/minimize a Sprite Kit app to avoid that exception?
I tried this within the view controller presenting the scene, but it does not get called:
-(void)viewWillDisappear:(BOOL)animated
{
SKView * skView = (SKView *)self.view;
skView.paused = YES;
[super viewWillDisappear:animated];
}
I found out that there's Kobold Kit sprite engine built on top of Sprite Kit, after porting my project to that, i can minimize the app and restore it with the same stuff on the screen!
I believe that the proper way to handle minimizing an app is in the AppDelegate via these methods :
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
Related
I have a toggle menu which toggle between the words "Pause" and "Resume" when it is pressed, which also pause and resume the whole game. This means when playing, the menu will be shown as "Pause" (tap here to pause), when pausing the menu will be shown as "Resume" (tap here to resume).
Here is the problem, if I tap the home button after I pause the game, then go back into it, it resumes itself and the pause menu is shown as "Resume". And this doesn't make sense to me. The best way I want is to pause the game whenever go into the background and resume from background. I look at the following methods, but they don't really work:
-(void)applicationWillResignActive:(UIApplication *)application{
}
- (void)applicationDidBecomeActive:(UIApplication *)application{
}
-(void)applicationWillEnterBackground:(UIApplication*)application{
}
-(void)applicationDidEnterBackground:(UIApplication *)application {
}
I even just put CCLOG in all of those methods, but nothing has been called. Is there something I am need to put/declare before I use those methods?
Sorry, it is a bit too long to read. Hope you can help me. Thank you.
I'm not sure why, but for my experience (not cocos2d), there's no additional implementation if want apply those methods.
perhaps, you should try look at this.
link 1 & link 2
-(void)applicationWillResignActive:(UIApplication *)application{
}
- (void)applicationDidBecomeActive:(UIApplication *)application{
}
you need custom your cocos2d CCDIRECTOR class. And get now display layer. then active it pause or resume function. All this must need some protocol.
These methods are invoked when the iOS forces your application in the background, or resumes execution of your application, ie they are signals you receive when an external event causes your application to go to background, or come back from it. You should not try to invoke them directly. There is no real relationship with a 'user created' menu like yours (like your Resume/Pause menu), unless you make the relationship explicit.
So , in the following method:
- (void)applicationWillResignActive:(UIApplication *)application {
NSLog(#"<%#>:applicationWillResignActive - received signal, pausing sharedDirector.",[self class]);
// here : place your code for forcing your menu in the 'Resume' state
// i am assuming some kind of change in a button, and
// a state variable of your own that define and control
// what it means to be 'paused' from your applications point of
// view
// then force the director to pause (animations, scheduling, touch, etc ...)
[[CCDirector sharedDirector] pause];
}
after, when the iOS hands you back control by placing your application in the forground as the running application:
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(#"<%#>:applicationDidBecomeActive - received signal, resuming sharedDirector.",[self class]);
[[CCDirector sharedDirector] resume];
}
You dont really need to do anything fancy here other than restart the CCDirector, since your menu is in the 'Resume' state, garanteed. When user presses Resume, you will start your game again and put the menu in the 'Pause' state.
My application is a coupon shopping,users will download the coupons in phone and when completed they will be directed for the automatic cash payment.
If a user downloads 5 coupons and inbetween gets a call as it is iOS4 it goes to background.
So when we press the home button also the application goes to background by this behaviour.I have save few data and restore the coupons when the user quits the application by homebutton.
But in iOS 4 behaviour homebutton press and phone call interruptions shows the same behaviour and calls the same functions,how i could differentiate between the 2.
Please this is a tedious function,please help me.......
Without multi-tasking: For applications that do not support background execution or are linked against iOS 3.x or earlier, applicationWillTerminate: method is always called when the user quits the application. For applications that support background execution, this method is generally not called when the user quits the application because the application simply moves to the background in that case.
With multi-tasking: You can implement applicationWillResignActive:delegate method in your app delegate, which gets called during temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
- (void)applicationWillResignActive:(UIApplication *)application {
flag = YES;
[self doCleanUp];
}
- (void)applicationWillTerminate:(UIApplication *)application {
if (!flag) [self doCleanUp];
}
iOS will not tell an app what caused an interruption such as a phone call, an SMS message, or a press of the home button. This is a deliberate design decision by Apple. Apple expects apps to be designed to exhibit the same behavior no matter what caused the interruption.
I have a NSTimer that I am stopping when my application resigns active, which I later restart when the application again becomes active. What I did not realise was that applicationWillResignActive would fire when my application first started (pretty obvious really). So what is happening is my NSTimer is incorrectly starting (when the application first starts). My question is, is there a way to check for the application resuming from inactive as apposed to it first starting?
- (void)applicationWillResignActive:(UIApplication *)application {
// STOP CORE TIMER
[[[self mapController] coreTimer] invalidate];
[[self mapController] setCoreTimer:nil];
}
.
- (void)applicationDidBecomeActive:(UIApplication *)application {
// START CORE TIMER
[[self mapController] setCoreTimer:[NSTimer
scheduledTimerWithTimeInterval:ONE_SECOND
target:[self mapController]
selector:#selector(timerDisplay:)
userInfo:nil
repeats:YES]];
}
There is the applicationWillEnterForeground which seems to only fire when the app comes back from the background. Just tested it, won't be called on launch.
Discussion
In iOS 4.0 and later, this method is
called as part of the transition from
the background to the active state.
You can use this method to undo many
of the changes you made to your
application upon entering the
background. The call to this method is
invariably followed by a call to the
applicationDidBecomeActive: method,
which then moves the application from
the inactive to the active state.
I'm planning to implement multi-task in my app.
I can see many methods here to do that in the AppDelegate like applicationWillResignActive, applicationDidEnterBackground, applicationWillEnterForeground, ...
But.... I don't see the way they should be used, nor why they are not in the ViewControllers... Nor what they are here for.
I mean : when the app enter in background, i don't know on which view my user is.
And back, when the app comes into foreground, how would I know what to do and what I may call, to update the view for example ?
I would have understood if those methods where in each view controller, but here, I don't see what they can be used for in a concrete way...
Can you help me to understand the way to implement things into those methods ?
Each object receive a UIApplicationDidEnterBackgroundNotification notification when the app goes in background. So to run some code when the app goes in background, you just have to listen to that notification where you want :
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(appHasGoneInBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
Don't forget to release the listener when you don't need to listen to it anymore :
[[NSNotificationCenter defaultCenter] removeObserver:self];
And best of the best, you can play the same way with the following notifications :
UIApplicationDidEnterBackgroundNotification
UIApplicationWillEnterForegroundNotification
UIApplicationWillResignActiveNotification
UIApplicationDidBecomeActiveNotification
They are not in any of your view controllers because iOS adopts a 'delegate' design pattern, where you can be assured that a method will fire upon a class (in this case, the App Delegate for your application) when required.
As a learning process, why don't you just put NSLog's in those methods to see when they're fired?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
NSLog(#"didFinishLaunchingWithOptions");
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
NSLog(#"applicationWillResignActive");
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
*/
NSLog(#"applicationDidEnterBackground");
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
/*
Called as part of transition from the background to the active state: here you can undo many of the changes made on entering the background.
*/
NSLog(#"applicationWillEnterForeground");
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
NSLog(#"applicationDidBecomeActive");
}
- (void)applicationWillTerminate:(UIApplication *)application
{
/*
Called when the application is about to terminate.
See also applicationDidEnterBackground:.
*/
NSLog(#"applicationWillTerminate");
}
In my app I am using CLLocationManager and AdWhirl. I have made no specific development regarding background mode: I don't want my app to be working when it is in background, i.e. when the user press the "home button", GPS location should no be updated.
Yesterday evening I pressed "home button", and this morning the iPhone was out of battery. It's an iPhone 4 with iOS 4.1, not jailbreaked, and there is no background app running.
The battery was about 35% yesterday evening, and 0% this morning (iPhone was shutdown).
I have set breakpoint in my delegate, which is called each time GPS location is updated. When app is in background mode, delegate is not called. So I'm thinking GPS is really disabled in background mode: ok.
This morning, I am following battery drain: it's about 1% drop each 15 min. I think it a bit too much.
Should I do something specific when the app goes to background mode? Do you think this 1% drop is normal?
Yes, internet access and GPS are two big drains on battery. I don't know at all what you mean with normal, since no other apps are running you've concluded that that is in fact what happens :) Assuming you've tested with NO apps running and didn't get 1% per 15 minutes...
For adwhirl, it's unknown whether it already stops accessing the internet when the app goes into the background, but you can add this to your App Delegate:
- (void)applicationDidEnterBackground:(UIApplication *)application {
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
*/
[lm stopUpdatingLocation];
[adView ignoreAutoRefreshTimer]
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
/*
Called as part of transition from the background to the active state: here you can undo many of the changes made on entering the background.
*/
[adView doNotIgnoreAutoRefreshTimer]
[lm startUpdatingLocation];
}
(lm and adView are the Location Manager object and the adWhirlView, both declared in the App Delegate. I've found it more useful to do all location managing via methods I make in the App Delegate.)