How can I perform an action after hitting the home button?
I guess I have to add some code to the AppDelegate?
Is it possible to do some code like?:
[self performSelector:#selector(selector)];
Thanks in advance!
Use applicationWillResignActive: method in appDelegate.m
- (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.
}
Or second option is to use applicationDidEnterBackGround:
- (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.
}
Depends ofcourse, what you want to do. Those comments are straight quotes from Apple's code template.
To access your view controller's methods:
MyViewController *vController = (MyViewController *)[_window rootViewController];
[vController pause];
Your error told you that you don't have pause method in your App Delegate class.
Swift 3:
func applicationWillResignActive(_ application: UIApplication) {
// 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 invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// 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.
}
Related
To recognize my app entered Background Mode [One short Home-Button click] I use this function:
func applicationDidEnterBackground(_ application: UIApplication) {
//...
}
But how can I recognize my app entered App Switching Mode [Two short Home-Button clicks]? The method applicationDidEnterBackground is not called in this case.
Try this:
func applicationWillResignActive(_ application: UIApplication) {
// 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 invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
Swift 2.3, SpriteKit
I know about AppDelegate Functions:
applicationWillResignActive and applicationDidEnterBackground
I have the following code:
func applicationWillResignActive(application: UIApplication) {
// 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.
mainScene.view?.paused = true
}
func applicationDidEnterBackground(application: UIApplication) {
// 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.
mainScene.view?.paused = true
}
With applicationDidEnterBackground it works fine. I'm able to pause the scene by tapping Home Button and resume it after.
But with applicationWillResignActive it doesn't work. When I run my game and receive Phone Call, the game doesn't pause. It keeps running in the background, so when I'm done with call and go back, I can see all my nodes in a completely mess.
before willResignActive
After
I used print statement to check if applicationWillResignActive works when I receive a phone Call and it shows me that it is executing, but the app just doesn't pause.
Also I've noticed that when I tap Home Button the app call applicationWillResignActive and then applicationDidEnterBackground functions. When I receive a phone call only applicationWillResignActive function is called.
So my question is - how can I deal with phone calls and pause my App with applicationWillResignActive function. I don't understand why I can pause it when I tap Home Button and can't when I get a Phone Call.
I was wondering if anyone knew how to execute a command when the user double presses the home button and/or when the just press it to go to the home screen. Snapchat does this as it blurs out the background when double tapped or dismisses a view controller if you go out and back in.
Using Xcode 7.3.1 and if possible could the code be Swift 2.2 or 2.3
Thanks
Regards,
In your Appdelegate.swift file you will find two methods that allow you to execute code when your app changes from the foreground state to the background - like pressing the home button.
func applicationWillResignActive(_ application: UIApplication) {
// 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 invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// 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.
}
plz use your AppDelegate methods
func applicationWillResignActive(application: UIApplication) {
// 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.
}
func applicationDidEnterBackground(application: UIApplication) {
// 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.
}
I have been working on a game in swift and when the home button is tapped accidentally I would like to run a function that restarts the game. Does anyone know what code needs to be implemented to call a function when the home button is tapped and the app is closed. (by home button I mean the actual hardware button)
You can use one of the following methods in your AppDelegate
// Taken straight from the AppDelegate template
func applicationWillResignActive(application: UIApplication) {
// 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.
}
func applicationDidEnterBackground(application: UIApplication) {
// 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.
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
// Saves changes in the application's managed object context before the application terminates.
self.saveContext()
}
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