iOS app detects phone lock screen and unlocks status

Source: Internet
Author: User

The iphone's lock screen monitoring is monitored in two ways:

1. Procedures in the foreground, this is relatively simple. Direct use of the Darwin Layer notification is available:

#import <notify.h>

#define notificationlock cfstr("Com.apple.springboard.lockcomplete")

#define notificationchange cfstr("Com.apple.springboard.lockstate")

#define Notificationpwdui cfstr("Com.apple.springboard.hasBlankedScreen")

static void screenlockstatechanged(cfnotificationcenterref Center,void* Observer,cfstringref name,const void* object, Cfdictionaryref userInfo)

{

nsstring* lockstate = (__bridge nsstring*)name;

if ([lockstate isequaltostring:(__bridge nsstring*)Notificationlock ]) {

NSLog(@"locked.") );

} else {

NSLog(@"lock state changed.") );

}

}

- (BOOL)application:(uiapplication *)Application Didfinishlaunchingwithoptions:(nsdictionary *)launchoptions

{

//Override point for customization after application launch.

Cfnotificationcenteraddobserver (Cfnotificationcentergetdarwinnotifycenter (), NULL, screenlockstatechanged, Notificationlock, NULL, cfnotificationsuspensionbehaviordeliverimmediately);

Cfnotificationcenteraddobserver (Cfnotificationcentergetdarwinnotifycenter (), NULL, screenlockstatechanged, Notificationchange, NULL, cfnotificationsuspensionbehaviordeliverimmediately);

SETSCREENSTATECB ();

return YES;

}

2. The second is the program back to the stage, then lock the screen will not receive the above notification, need another way, in a circular way has been to detect whether the lock screen state, will consume performance and may be Apple hang;

static void SETSCREENSTATECB()

{

  

uint64_t locked;

__block int token = 0;

Notify_register_dispatch("com.apple.springboard.lockstate",&token,dispatch_get_ Main_queue(), ^ (int t) {

});

Notify_get_state(token, &locked);

NSLog(@"%d", (int)locked);

}

- (void)applicationdidenterbackground:(uiapplication *) Application

{

while (YES) {

SETSCREENSTATECB();

Sleep(1);

}

}

About Notify.h please go to:

Https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/notify_register_check.3.html

iOS app detects phone lock screen and unlocks status

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.