Tometoyou
Tometoyou

Reputation: 8376

Confused over which application state notifications I should be observing

My app is a livestreaming app that uses voice and video. I want to:

  1. Detect when a user has brought up Notification Centre/Control centre over the app
  2. Detect when the user receives some kind of full screen notification like battery low
  3. Detect when the user receives a phone call
  4. Detect when a user has pressed the home button to background the app
  5. 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:

  1. .willResignActiveNotification
  2. .willResignActiveNotification
  3. .willResignActiveNotification or .didEnterBackgroundNotification?
  4. .didEnterBackgroundNotification
  5. .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?

Upvotes: 3

Views: 1923

Answers (1)

Bhavin Kansagara
Bhavin Kansagara

Reputation: 2916

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:

  1. Detect when the user receives a phone call

    only .willResignActiveNotification will be fired.

  2. Detect when a user has pressed the home button to background the app

    both .willResignActiveNotification and .didEnterBackgroundNotification will be fired respectively.

Hope it helps.

Upvotes: 6

Related Questions