Reputation: 833
How do I programmatically lock and unlock the main screen (i.e. the device itself) of an iPhone?
Upvotes: 16
Views: 23233
Reputation: 779
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...
Upvotes: 0
Reputation:
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.
Upvotes: -1
Reputation: 246
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);
}
Upvotes: 4
Reputation: 2453
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
Upvotes: 1
Reputation: 14509
It's not possible. However, you can "prevent" your phone from locking when your app is running. [UIApplication sharedApplication].idleTimerDisabled = YES
should do it.
Upvotes: 26
Reputation: 21571
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.
Upvotes: 2
Reputation:
It can be done by caling GSEventLockDevice (); from your app. This function can be found in GraphicsServices.framework.
Upvotes: 23
Reputation: 2937
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.
Upvotes: 0
Reputation: 3969
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.
Upvotes: 0