0

how to close screen(device go to sleep) when user click a button.

I already tried below code,but not work.

[UIApplication sharedApplication].idleTimerDisabled = YES;

and

NSDate *future = [NSDate dateWithTimeIntervalSinceNow: 0.06 ];
[NSThread sleepUntilDate:future];
Abizern
  • 146,289
  • 39
  • 203
  • 257
nang531
  • 65
  • 6
  • 3
    You mean, you want to send the device to sleep (block the screen)? It's not possible. – FreeNickname Feb 07 '15 at 14:48
  • As for your code, the first line forces the device's screen to always be on (the opposite of what you want, I believe), and the second one just freezes the current thread for 60 milliseconds. – FreeNickname Feb 07 '15 at 15:00
  • Why do you want to implement such functionality? – FreeNickname Feb 07 '15 at 15:01
  • @FreeNickname,Because my fd want a app that he click the app,let the screen of device to sleep.so i make it to him – nang531 Feb 07 '15 at 15:11
  • so send the device to sleep, It's not possible? can do that use ios private api or other way? – nang531 Feb 07 '15 at 15:15
  • It might be possible via private APIs/jailbreak. This might help you: [iOS private API lock device and power off the screen](http://stackoverflow.com/questions/14862328/ios-private-api-lock-device-and-power-off-the-screen). I, personally, unfortunately, didn't have much experience with private APIs/jailbreak, so I'm unlikely to help you on this matter. – FreeNickname Feb 07 '15 at 15:24

1 Answers1

1

If you are willing to use private API (will not be accepted by Apple), there is a solution.

Use GSEventLockDevice in the GraphicsServices.framework

This solution works, but you won't be able to put your App on AppStore, it will only be OK for internal uses Apps

MORE ABOUT YOUR CODE :

[UIApplication sharedApplication].idleTimerDisabled = YES;

Is for prevent your device to lock after the Idle time set by user on his Settings.

NSDate *future = [NSDate dateWithTimeIntervalSinceNow: 0.06 ];
[NSThread sleepUntilDate:future];

Putting a Thread to sleep, has nothing to do with locking a device. You probably experienced an app Freeze for about 0.06 second when testing this...

TheSquad
  • 7,385
  • 8
  • 40
  • 79