Confused over which application state notifications I should be observing - ios

My app is a livestreaming app that uses voice and video. I want to:
Detect when a user has brought up Notification Centre/Control centre over the app
Detect when the user receives some kind of full screen notification like battery low
Detect when the user receives a phone call
Detect when a user has pressed the home button to background the app
Detect when the app terminates.
I'm a bit confused as to which notifications I should be observing to detect these events.
My guess is:
.willResignActiveNotification
.willResignActiveNotification
.willResignActiveNotification or .didEnterBackgroundNotification?
.didEnterBackgroundNotification
.willTerminateNotification
And to detect when the app is back in its active state for 1 to 4 I need .didBecomeActiveNotification?
Is this right? Which one is number 3?

Yes, You should observer .willResignActiveNotification because your application still exists below iOS's Phone Application, which is presented by iOS when there is an incoming call. .didEnterBackgroundNotification will not be fired on incoming call, it will be fired when you press the home button.
Now, once you done with the call either by rejecting it or after finish your talk the Phone Application of iOS is removed from top and make your application active. So there you can observe for .didBecomeActiveNotification for all cases.
You can also check the commented lines in the methods provided by Xcode, when you create a new project. Checkout AppDelegate.swift to understand the difference
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.
}
Summarising it with your cases:
Detect when the user receives a phone call
only .willResignActiveNotification will be fired.
Detect when a user has pressed the home button to background the app
both .willResignActiveNotification and .didEnterBackgroundNotification will be fired respectively.
Hope it helps.

Related

Detect app state in iOS when occurring incoming call or any events on top of the app?

AppState is not notifying in iOS when any activity comes on top of app.
inactive - This is a state that occurs when transitioning between foreground & background, and during periods of inactivity such as entering the Multitasking view or in the event of an incoming call.
How to handle app when any events comes on top of an app like the incoming call events?
Please try below method to determine application status.
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.
}

How to Remove Repeating Local Notifications Wisely (Swift 4)?

I have an app that triggers local notifications every 30 minutes from the moment a certain button is tapped (both in the foreground and the background). I learned how to remove those notifications when another button is tapped or when the view controller is dismissed. This is done through:
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
It works just fine. However, if the user force-quits (shuts down manually) the app by swiping up in multitasking mode, this is when strange things start happening. By default, the terminated app doesn’t remove those pending push notifications and they keep coming even if the app is closed.
What I learned is that there are several states in the app’s lifecycle in which the app can be killed. Accordingly, there are several methods in the App Delegate that we can use to trigger the mentioned code to remove the notifications (otherwise they keep coming).
So, when we are in foreground and tap the home button (or swipe up with iPhone X) to switch to multitasking mode (choosing between active apps or deleting them), this is the time for applicationWillResignActive – we can start observing for app termination in here (maybe by means of observing applicationWillTerminate and using a certain method as a selector) and remove those notifications upon terminating (strangely enough, applicationWillTerminate doesn’t always work on its own).
However, if you switch to another app and then go back to multitasking mode, the app is in background mode this time (not inactive mode). So, there seems to be no way to trigger certain methods and observe for certain things when the app is already in background mode. I wonder if any? In this case applicationDidEnterBackground doesn’t work either. Everything ends up with “Message from debugger: Terminated due to signal 9”
So, how do you remove all pending notifications when your app is in background mode and is about to be terminated by the user? If it’s in background mode and is not going to be terminated by the user, the notifications need to stay active.
If there is a solution, please help. Or maybe there is a better way to remove pending (active) local notifications when the view controller is dismissed (it’s not the only and main view controller in the app) and when the user terminates/shuts down the app in inactive or background state? Thanks for your help.
You have few options for when the app is about to become NOT active:
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 applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
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.
}
In your case, you should probably remove notifications in all of them, and add them again in the other functions that complement them, for example:
func applicationDidBecomeActive(_ application: UIApplication) {
// 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.
}
I believe it is good practice to add and remove the notifications in the UIViewController life cycle methods, instead of in the app level life cycle

Swift, pause app with applicationWillResignActive

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.

When app switcher appears execute a command Xcode swift

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 Wanted To Get Notified By Local Notification When My App Is In Background Or Else In Foreground

I have a PrayerButton in my drawer menu, On tapping it goes to
PrayerViewController, and in PrayerViewController
i have 5 Prayer Timings where i created a UI local notification method and fired those timings in firedate and sound too. now my query is.
where should i call my notification method.
“All i want is my local notification method should call when my app is in Background, foreground and it should continuously running like a service, so that whenever those 5 Prayer time will come on firedate then it should alert me by UI Local Notification banner (when in foreground) and if app is in background it should notify me".
i am a beginner in Stackoverflow might be my way of asking question will not be right but i tried asking Question.
sorry for the mistakes
and welcome to any reply
thanks in advance..
Use these app delegate methods
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 applicationWillEnterForeground(application: UIApplication) {
// 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.
}

Resources