3

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?

Tometoyou
  • 7,792
  • 12
  • 62
  • 108
  • I would not use willResignActiveNotification only, i think you should do research for each interruption, by user or system. As i understand if you just want to pause/play video, just use didEnterBackeground and didBecomeActive to cover all the cases. – Markicevic Jan 18 '19 at 14:18

1 Answers1

6

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.

Bhavin Kansagara
  • 2,866
  • 1
  • 16
  • 20
  • My problem is I need to distinguish between a phone call that will interrupt my audio input to the livestream, and a full screen notification that also calls `.willResignActiveNotification`. How would I do that? – Tometoyou Jan 18 '19 at 17:53
  • okay, for that you should go through CallKit, if there is something to help you distinguish between the two. https://developer.apple.com/documentation/callkit – Bhavin Kansagara Jan 21 '19 at 07:13
  • https://stackoverflow.com/questions/36014975/detect-phone-calls-on-ios-with-ctcallcenter-swift/42752727#42752727 Here there is way to detect for incoming call, have a look. – Bhavin Kansagara Jan 21 '19 at 07:15
  • There is the delegate method, `applicationWillResignActive(_:)` and `willResignActiveNotification` broadcast notification. Which one should I use - Delegate method or Notification? I'm unable to choose even after reading their documentation. When I tested them in code, both the delegate method and its corresponding notification is fired... I don't need both.. but what is the recommendation? – NightFuryLxD Aug 29 '23 at 15:35