Using Undeclared Identifier "UIApplicationSignificantTimeChangeNotification"

advertisements

I'm working on implementing notifications for the first time. I'm having issues at this line:

[noteCenter addObserver:self selector:@selector(timeChangedSignificantly:)
                           name:UIApplicationSignificantTimeChangeNotification object:nil];

From this code:

if (self)
    {
        _stepCounter = [[CMStepCounter alloc] init];
        self.stepsToday = -1;

        NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter];

        // subscribe to relevant notifications
        [noteCenter addObserver:self selector:@selector(timeChangedSignificantly:)
                           name:UIApplicationSignificantTimeChangeNotification object:nil];
        [noteCenter addObserver:self selector:@selector(willEnterForeground:)
                           name:NSExtensionHostWillEnterForegroundNotification
                         object:nil];
        [noteCenter addObserver:self selector:@selector(didEnterBackground:)
                           name:NSExtensionHostDidEnterBackgroundNotification
                         object:nil];

        _stepQueue = [[NSOperationQueue alloc] init];
        _stepQueue.maxConcurrentOperationCount = 1;

        // start counting
        [self _updateStepsTodayFromHistoryLive:YES];
    }


Your use of NSExtensionHostWillEnterForegroundNotification suggests that this code is supposed to be running in an app extension (something that you mysteriously neglected to mention in your question).

Well, an extension has no access to UIApplication - or its notifications. See the section in Apple's docs called "Some APIs Are Unavailable to App Extensions":

https://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensionOverview.html#//apple_ref/doc/uid/TP40014214-CH2-SW6