I have requirement in my application to detect if phone screen is on/off when the application is in the background? I found that this can be possible using private framework Spring board. Can we do so with public APIs?
Thank.
try this:
in AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Other code
[self registerforDeviceLockNotif];
}
//Register Notification
-(void)registerforDeviceLockNotif
{
//Screen screenDisplayStatus notifications
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, screenDisplayStatus, CFSTR("com.apple.iokit.hid.displayStatus"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
NULL, // observer
screenLockStatus, // callback
CFSTR("com.apple.springboard.lockstate"), // event name
NULL, // object
CFNotificationSuspensionBehaviorDeliverImmediately);
}
//RemoveNotification if you don't need any more.
-(void)removeforDeviceLockNotif{
CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, CFSTR("com.apple.iokit.hid.displayStatus"), NULL);
CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, CFSTR("com.apple.springboard.lockstate"), NULL);
}
//Call back Methods
static void screenDisplayStatus(CFNotificationCenterRef center, void* observer, CFStringRef name, const void* object, CFDictionaryRef userInfo) {
uint64_t state;
int token;
notify_register_check("com.apple.iokit.hid.displayStatus", &token);
notify_get_state(token, &state);
notify_cancel(token);
if (state) {
screenIsBlack = NO;
}else{
screenIsBlack = YES;
}
}
static void screenLockStatus(CFNotificationCenterRef center, void* observer, CFStringRef name, const void* object, CFDictionaryRef userInfo)
{
uint64_t state;
int token;
notify_register_check("com.apple.springboard.lockstate", &token);
notify_get_state(token, &state);
notify_cancel(token);
if (state) {
screenIsLocked = YES;
}
else
{
screenIsLocked = NO;
}
}
Do you task when screen is black screen:
if (appIsBackground && (screenIsBlack || screenIsLocked) {
//do Task.
}
Note that, here I have made the screen lock status as screen is black, if you need not, just remove the lock status judgement.
Also consider that an app in a background mode will only run / do tasks for a certain period of time before it is suspended. Then all your code stops executing and the app is "paused".
If your app offers features like GPS tracking, Audio playback - that sort of thing, then you can request permission from Apple to keep it alive in the background. If it doesn't offer the above or something similar - then your app will only live a very, undetermined, short time in the background thus, detecting the screen state may be a fruitless exercise.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.