Is there a way to check if the iOS device is in sleep mode?

Hi All,


I have used GPS location updates in my application. I want to detect if the iOS device is in sleep mode so that I can turn off the GPS and optimize the battery use. I have already tried pausesLocationupdates in iOS 6, but it does not work as desired.

I want to turn off the GPS as soon as the device goes to sleep mode. Is there any way to achieve this functionality ?


Thanks in advance.

iPhone 4S, iOS 6, pausesLocationUpdatesAutomatically

Posted on Jan 8, 2013 10:23 PM

Reply
5 replies

Jan 8, 2013 10:29 PM in response to Rohit_Kashyap

If iOS goes to sleep, your app will receive a notification automatically.

What your app does after that is whatever you've coded it to do.

When iOS wakes up, your app will also receive a notification automatically.

What your app does after that is whatever you've coded it to do.


iOS handles powering up/down the various radios automatically. You don't have to worry about it.

Jan 8, 2013 10:47 PM in response to msuper69

Thanks Michael.


Could you please mention which notification will my app receive?

"resignactive" will only be called when application goes to background, however I want to catch the scenario when my application is already running in the background and the device goes to sleep, the screen blackens, at that time I want to turn off the GPS, so please mention which notification will my application get in this scenario ?

Jan 9, 2013 10:00 PM in response to Rohit_Kashyap

I want to detect the lock/unlock events in the device, so far I got the darwin notifications as given below,:


-(void)registerForDarwinNotif

{




//Screen lock notifications

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center

NULL, // observer

displayStatusChanged, // callback

CFSTR("com.apple.iokit.hid.displayStatus"), // event name

NULL, // object

CFNotificationSuspensionBehaviorDeliverImmediately);

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center

NULL, // observer

displayStatusChanged, // callback

CFSTR("com.apple.springboard.lockstate"), // event name

NULL, // object

CFNotificationSuspensionBehaviorDeliverImmediately);

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center

NULL, // observer

displayStatusChanged, // callback

CFSTR("com.apple.springboard.hasBlankedScreen"), // event name

NULL, // object

CFNotificationSuspensionBehaviorDeliverImmediately);


}


//My call back

static void displayStatusChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)

{

NSLog(@"IN Display status changed");

// you might try inspecting the `userInfo` dictionary, to see

// if it contains any useful info

NSLog(@"Darwin notification NAME = %@",name);

}


I am able to get the darwin notifications when device is locked/unlocked, but the real problem is how identify between if the notification has come from locking or the unlocking of the device. Console logs are:


Jan 10 11:29:01 Frost-bank LockDetectDemo[2086] <Warning>: IN Display status changed

Jan 10 11:29:01 Frost-bank LockDetectDemo[2086] <Warning>: Darwin notification NAME = com.apple.springboard.lockcomplete

Jan 10 11:29:01 Frost-bank LockDetectDemo[2086] <Warning>: IN Display status changed

Jan 10 11:29:01 Frost-bank LockDetectDemo[2086] <Warning>: Darwin notification NAME = com.apple.springboard.lockstate

Jan 10 11:29:01 Frost-bank LockDetectDemo[2086] <Warning>: IN Display status changed

Jan 10 11:29:01 Frost-bank LockDetectDemo[2086] <Warning>: Darwin notification NAME = com.apple.springboard.hasBlankedScreen

Jan 10 11:29:01 Frost-bank LockDetectDemo[2086] <Warning>: IN Display status changed

Jan 10 11:29:01 Frost-bank LockDetectDemo[2086] <Warning>: Darwin notification NAME = com.apple.iokit.hid.displayStatus

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Is there a way to check if the iOS device is in sleep mode?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.