0

As we all know, applicationDidBecomeActive will call when we open and close the Bottom Control Center / Top Noficication Center.

But I want to know in the applicationDidBecomeActive when only because of these 2 events, to handle some functionality when user opens and close Notification Center or Control Center.

- (void)applicationDidBecomeActive:(UIApplication *)application
{

    if(/*Code for DidBecomeActive Called Because of Contol Center*/ --- )
    {

    }


if(/*Code for DidBecomeActive Called Because of Notification Center*/)
    {

    }


}

Any one help me to findout

Code for DidBecomeActive Called Because of Notification Center

Code for DidBecomeActive Called Because of Contol Center

iOS dev
  • 2,254
  • 6
  • 33
  • 56
  • Here you may find your [information](http://stackoverflow.com/questions/7733730/how-to-determine-in-applicationdidbecomeactive-whether-it-is-the-initial-iphone). – voltae Feb 16 '17 at 06:21

1 Answers1

5

I'm not sure you can achieve exactly what you want - but you can get close. When you pull the Notification Center down (or the Control Centre up, or enter App Switcher) you will get:

applicationWillResignActive

Closing the panel and returning to the app will call:

applicationDidBecomeActive

Now fully backgrounding the app instead calls this sequence:

applicationWillResignActive
applicationDidEnterBackground

And re-opening the app calls:

applicationWillEnterForeground
applicationDidBecomeActive

So all you need to do is use a flag to track the sequence:

@property (nonatomic, readwrite) BOOL wasControlCenter;

- (void)applicationWillResignActive:(UIApplication *)application {
    _wasControlCenter = YES;
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    _wasControlCenter = NO;
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    _wasControlCenter = NO;
}

- (void)applicationDidBecomeActive:(UIApplication *)application {

    if (_wasControlCenter) {
        // Do your thing
    }    
}

Unfortunately I don't think there's a way to differentiate between Control Center, Notification Center, App Switcher etc.

norders
  • 1,160
  • 9
  • 13
  • Testing with iOS13 applicationWillResignActive is not being triggered when going to control panel, but applicationDidBecomeActive does when you come back. – birdman Nov 06 '19 at 02:43
  • Successfully validated by seeing the differences of the callbacks between app going to OS settings by opening `openSettingsURLString` vs. just pulling down the control center / notification center. Findings: when you have control center / notification center, you're not backgrounding the app, **you can still see the app in a blurry way**, you're just resigning it from being active. When control center is let go, app becomes active again. However for going to the settings app, the events are such: resignActive --> EnterBackground and then upon returning to app: EnterForeground --> BecomeActive – mfaani Oct 13 '21 at 20:08