Pause main execution before iOS app terminates - ios

How can I make my app pause for a couple of seconds to invoke a message inside applicationDidEnterBackground when I hit the home button? For example:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self.delegate aMessage];
// Pause for a couple of seconds to give aMessage time to finish
// Continue execution after 2 seconds
}

You can't, but you don't need to either. Your app gets to finish aMessage before it really goes into the background at which point you can't execute any code anymore.

Related

Detect when app is minimized (iOS)

I am building a mobile game and I have a NSTimer for my stopwatch. Everything works fine, except when I minimize the app (press home screen), or when it is interrupted by, say a phone call, the NSTimer continues running in the background, when the application is not in use.
I need to invalidate this timer when the app is minimized/interrupted and create a new timer when the app is resumed. What methods handle when the app is minimized and resumed?
You can invalidate timer in AppDelegate in UIApplication delegate method applicationDidEnterBackground this way,
- (void)applicationDidEnterBackground:(UIApplication *)application {
[timer invalidate];
timer = nil;
}
And create a new timer in method applicationWillEnterForeground

Background Execution in Xcode

I am asking if I can repeat a method in Xcode after home button pressed. Let's say every 30 minutes.
I have no idea to do it if it is possible.
I think your question is related to background execution by saying after home button pressed. First you need to address how to call the function in the background.
Here is what I recommend for you to follow:
First:
I recommend you to take a look at the apple official document.
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
Second:
There is a good tutorial:
http://pinkstone.co.uk/how-to-execute-a-method-on-a-background-thread-in-ios/
Third:
Here is what you are asking:
http://chrisrisner.com/31-Days-of-iOS--Day-23%E2%80%93Using-Background-Threads
Fourth:
If you want to explore the advanced material:
http://mobiforge.com/design-development/using-background-fetch-ios
use NSTimer
[NSTimer scheduledTimerWithTimeInterval:"your time"
target:self
selector:#selector(your method:)
userInfo:nil
repeats:YES];
There are Methods (like "viewDidLoad") that gets called for these cases. Here are some:
- (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)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
you can place the NSTimer in one of these and anything else you want to do when the home button is pressed. Hope this helps.
Source: Detect when home button is pressed iOS

NSTimer not stopping after the app goes into background

Until yesterday I was sure, the NSTimer will get stopped after the app goes into background. I'm have a feeling like experiencing some anomally.
My app has update location and play audio background modes. Update location is refreshed every few seconds. But it only happends on one of the app screens. There also is NSTimer refreshing some UI.
I've scheduled it like this:
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(updateTime) userInfo:nil repeats:YES];
There is also a method, which content is irrelevant now:
-(void)updateTime
{
//irrelevant content, but the method gets fired even when the app is in background
}
The weird thing is, thah the method, which is only fired by the NSTimer and nowehere else is fired even after the ap go into the background. What is happening here? Is that normal behaviour?
Because you are using background modes with location and audio your app is still alive in background.and so your timers are running.
If you remove background modes with location and audio that you are using and then try the timers wont work.
Its Normal behaviour. Correct me if i'm wrong.
Create your timer as public i mean add it in .h file and access it when your app. enter in backGround Mode
- (void)applicationDidEnterBackground:(UIApplication *)application
By above method and set your timer as inValidate.. its fix.
And if you want to do again start your timer then you can access it by
- (void)applicationWillEnterForeground:(UIApplication *)application
this method. Here you need to recreate your timer.
You can stop the timer using invalidate the timer.
[self.timer invalidate];
self.timer= nil;

AVPlayer and addPeriodicTimeObserverForInterval does not work when app goes to background

Question 1.
I am seeing following behavior on iphone 4 and iOS 5.0.1
register using addPeriodicTimeObserverForInterval to receive updates every 250 ms and update UI.
works well till the app goes to background. For eg. hit the home button or lock the screen,
When app comes back to foreground the player starts playback again but the updates dont fire again. If user hits the play pause button again on the UI updates start firing again.
This can be seen in the demo app from apple as well.
Question 2
Can we not mix C based Audio Session APIs with AV foundation classes? For eg. I have my C based listener registered when AudioInterruptions. But when I use AVPlayer with kAudioSessionCategory_PlayAndRecord they dont get called. When app goes to background AVPlayer pauses without the C based listener getting called.
Is this expected or can I do something? Please note that once I have disposed the AVPlayer instance and my app goes to background again wiht kAudioSessionCategory_PlayAndRecord
set as category the listener function gets invoked.
For your first problem, most likely you are not releasing the addPeriodicTimeObserverForInterval as suggested. Try putting something like this in your AppDelegate:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[_audioManager.audioPlayer removeTimeObserver:_audioManager.timeObserver];
_audioManager.isUIActive = NO;
}
and the appropriate method to restart the periodic time observer:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[_audioManager setUpTransportUI];
_audioManager.isUIActive = YES;
}
where setUpTransportUI recreates your time observer.

ipad sleep wake up event

When the ipad goes to sleep i.e. the screen turns off, the user will turn it back on and the current application at the time is still open. Is there a way to detect this event that the ipad has woken up from sleep?
I want to be show an UIAlert whenever it goes to sleep and wakes up.
Take a look at the - (void)applicationDidBecomeActive:(UIApplication *)application delegate method of the application delegate.

Resources