How do I programmatically lock and unlock the main screen (i.e. the device itself) of an iPhone?
9 Answers
It's not possible. However, you can "prevent" your phone from locking when your app is running. [UIApplication sharedApplication].idleTimerDisabled = YES
should do it.
-
"it's not possible" seems not to be true (there's a tad bit of difference between "not possible" and "not documented"...) – The Paramagnetic Croissant Jul 31 '14 at 19:02
It can be done by caling GSEventLockDevice (); from your app. This function can be found in GraphicsServices.framework.
-
17
-
1@H2CO3 : It's not working in `iOS7`. Can you tell me about `iOS7`? I already asked a [Question](http://stackoverflow.com/q/19209887/1603072) for that. – Bhavin Oct 07 '13 at 10:54
-
@Vin I can't. I don't use iOS 7, and since this is a private API, it is expected to break. Maybe another private function can do the same thing. Or just hook SpringBoard. – Oct 07 '13 at 11:44
-
@H2CO3 : Disappointed. I am seeing you answering the Questions here since a year and I thought you will have some Answer for this problem. But no problem. Thanks for your reply. :) – Bhavin Oct 25 '13 at 13:00
-
3@Vin I know what you feel :) You see, I'd much rather spend my time buying the latest iDevices and digging deep into the top notch iOS APIs and technologies instead of having to go to uni (where I'm forced to visit an "introduction to programming" class, where we learn C++ without pointers...). But unfortunately, that's not an option, and I hardly ever have the time to do iOS development... :/ – Oct 25 '13 at 13:19
-
`GSEventLockDevice` only locks the device. How to unlock? Is it posible with private eAPI only? – Maxim Shoustin Apr 23 '14 at 09:00
This has already been resolved . You can find it on Github: https://github.com/neuroo/LockMeNow (work below IOS 7)
char*framework="/System/Library/PrivateFrameworks/GraphicsServices.framework/GraphicsServices";
void *handle= dlopen(framework, RTLD_NOW);
if (handle)
{
void (*GSEventLockDevice)() = dlsym(handle, "GSEventLockDevice");
if (GSEventLockDevice)
{
GSEventLockDevice();
NSLog(@"Phone is Locked");
//.........
}
dlclose(handle);
}

- 8,100
- 16
- 64
- 86

- 246
- 3
- 12
It is probably possible with undocumented Apple functions (maybe GSEventLockDevice() ?) but it certainly leads to automatic App Store REJECTION.
Apple simply don't want anyone to fiddle with core functionalities like this.

- 21,461
- 5
- 90
- 86
If you want to do this so, Apple never approve this, your app must be jailbreak. you can do this by calling Private framework on your project. you can use GraphicsServices.framework
.
NOTE :
This GraphicsServices.framework
is a private framework. Apple will never accept your app. By calling GSEventLockDevice()
method you can lock or unlock your Device easily. This GSEventLockDevice()
resides in the GSEvent.h
.
I hope this one helps you.
Please let me know if you still facing any problem
-
i did this successfully, but how can i set or change passcode to phone before lock it. thanks in advance. – M.Shuaib Imran Aug 26 '13 at 07:43
-
it basically isn't possible because this probably is part of the private frameworks which can be used by Apple only. There are apps such as the fake caller apps that utilize a "fake" lockscreen but as you've pointed out, pressing the home button quits the app, rendering your lock screen useless.

- 2,937
- 2
- 35
- 69
Now there is a workaround if you really need lo be able to lock the phone but I have no solution to unlock it...
With iOS 16.4 you can now use the "Lock Screen" action in shortcuts.
So to lock a device programmatically you can ask your app to launch a shortcut that will do the job.
You can import the needed "Lockscreen" shortcut with one single line of code:
openURL(URL(string: "https://www.icloud.com/shortcuts/da76168e73974887ae96480d435da049")!)
When user did import the needed shortcut, you can call it directly:
func runLockscreenShortcut2() {
if let url = URL(string: "shortcuts://run-shortcut?name=Lockscreen"), UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
Only drawback is that the Shortcuts app will appear briefly before the device locks, and will be in foreground when unlocking the briefly.
You can add a "Go To Home Screen" action to your shortcut before the "Lock Screen" action, so that Shortcut app won't be in foreground when device will unlock but it will slow down locking process.
Far from perfect but your unique option for the moment...

- 709
- 6
- 14
I don't believe that there is a way to achieve this.
One thing that i believe is possible is to stop the IPhone from locking. you could then build a view that copied the lock unlock function and you would still have control over the phone.

- 3,901
- 4
- 35
- 51
Describe lock and unlock. I would try a switch that enabled = YES and enabled = NO for the view property. So basically you can disable all the UIGestureRecognizers
and 'lock' the screen, if this is what you mean. I do it with UIbuttons
once I add them as an IBOutlet
as well as IBAction
, so they are an object and can be modified at the property level. I am working on this very thing right now. I will post my findings.

- 232,980
- 40
- 330
- 338