3

I am trying to detect when a user locks the device (vs. pressing home button for instance).

Found this:

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
                                    NULL, // observer
                                    lockStateChanged, // callback
                                    CFSTR("com.apple.springboard.lockstate"), // event name
                                    NULL, // object
                                    CFNotificationSuspensionBehaviorDeliverImmediately);



static void lockStateChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
    NSLog(@"event received!");
    // you might try inspecting the `userInfo` dictionary, to see
    //  if it contains any useful info
    if (userInfo != nil) {
        CFShow(userInfo);
    }
}

I can imagine that com.apple.springboard.lockstate is like calling private API? Or is this fine?

Community
  • 1
  • 1
openfrog
  • 40,201
  • 65
  • 225
  • 373

1 Answers1

3

Assuming all the CF... functions are public you are probably OK, but in a murky area for sure. Next release of iOS could break your code if Apple changes that string.

What I did in a similar situation for an approved shipping app was to avoid using the string directly. Create an array of the strings, then use the NSString method to combine them with a period separator instead of using com.apple.springboard.lockstate directly.

YMMV

David H
  • 40,852
  • 12
  • 92
  • 138