6

I'm trying to auto lock the device after a given time period. The only thing I've seen that would make this possible is doing this:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    UIApplication.sharedApplication().idleTimerDisabled = true

    NSTimer.scheduledTimerWithTimeInterval(30, target: self, selector: "lockScreen", userInfo: nil, repeats: false)

    return true
}

func lockScreen() {
    print("locking screen")
    UIApplication.sharedApplication().idleTimerDisabled = false
}

However it doesn't seem to work. Are there any other alternatives? There is app on the market called CellControl that does this so I know it's possible, just can't seem to figure out how.

I've also tried in obj-c taken from this answer

Here is a clip of their app working which is downloaded from the public app store. You can see that as soon as I hit the home button and exit the app, they force lock the screen.

enter image description here

I've also seen using private frameworks which would most definitely call for rejection:

char *gsDylib = "/System/Library/PrivateFrameworks/GraphicsServices.framework/GraphicsServices";
void *handle = dlopen(gsDylib, RTLD_NOW);
if (handle) {
  BOOL locked = FALSE;
  void (*_GSEventLockDevice)() = dlsym(handle, "GSEventLockDevice");
  if (_GSEventLockDevice)  {
    _GSEventLockDevice();
    //...
  }
  dlclose(handle);
  //...
}

When first launching the app they ask for permission to:

  • Make data available to bluetooth devices even when not using the app
  • Send push notifications
  • Access contacts
  • Access microphone
  • Use location even when not using the app

I don't know if any of these frameworks would give you the ability to lock the screen but maybe?...


Quick update:

After some more research and huge help from JBA I'm getting closer to a solution. It seems that Cell Control is acting as keyboard peripheral allowing them to send a command to lock the screen. So I bought a bluetooth keyboard to try and guess what...works like charm. I'm able to lock and unlock my device from it. So I hooked the keyboard up to my mac (via Bluetooth) to sniff the packets. This event is logged when the lock button is pressed on the keyboard:

enter image description here

From what I can tell (I'm by no means an expert at this), is that to trigger a lock, all it sends is a mouse event with all event data zero'd out. Along with no buttons pressed either. My goal to replicate this on Arduino...so more work to be done.

Community
  • 1
  • 1
random
  • 8,568
  • 12
  • 50
  • 85
  • I was gonna say there's no way Apple would accept that into the App Store, but apparently they would. My guess is it slipped through because it's against their guidelines to use any private APIs which would be required to achieve this – Chris Feb 18 '16 at 18:06
  • I was guessing that it "slipped" through as well however in order to use the app you have to buy a $120 piece of hardware. Seems like an awful lot of risk to develop out an entire hardware platform and not even know if your app is going to be rejected or not. It seems like they have to be using something different – random Feb 18 '16 at 18:11
  • 2
    Either that, or they had an arrangement with Apple prior to building out their product to ensure it makes it through. If you can't find the answer you're looking for, you should have a couple technical tickets through your dev account that you can open up to ask an apple engineer – Chris Feb 18 '16 at 18:15
  • @random have you got any solution for this implementation ? – vishy Apr 07 '16 at 09:24
  • @vishy yeah, contacted apple dev support and there is no supported way to do it. – random Apr 07 '16 at 14:09
  • @random thanks for the response, have you worked on MDM using enterprise account, i want to know the approximate time for execution of command sent from our MDM server. – vishy Apr 19 '16 at 04:54
  • @vishy yeah I have, but couldn't tell you the execution time off the cuff. – random Apr 19 '16 at 13:30
  • @random by the time did you find anymore useful information about the structure / datas of the HID keyboard event that is sent to power-off the device? – JBA May 18 '16 at 16:31
  • @JBA I've not found anything much more useful unfortunately :( seems to be a bit more difficult that I thought. – random May 18 '16 at 18:56
  • @JBA I did post a more detailed question here that may help you: http://forums.adafruit.com/viewtopic.php?f=53&t=96664 – random May 18 '16 at 19:02

4 Answers4

10

If you want to know how they do this :

The phone is paired with the bluetooth device included in their hardware. If you check further, you will notice that this Bluetooth device has the "Keyboard" profile: just check on your phone, you will see it is recognized as a wireless keyboard... Interesting... Do you see the answer coming ? ...

You Bet ! The device sends to the phone the lock screen command-key as if it was a connected bluetooth keyboard (yes, because a BT Keyboard can actually do this). And here you go.

=== EDIT ===

Please take a look at this HID usage table, you will find some useful command codes. The key codes we're looking for are most probably 0x81 or 0x82.

JBA
  • 2,889
  • 1
  • 21
  • 38
  • ...wow. That's a really cool approach and never thought of it. I connected a bluetooth keyboard to an iPhone to test but can't seem to find any shortcut anywhere that will lock the phone. Do you happen to know what it is? – random May 02 '16 at 15:36
  • I've been searching around and tried all the shortcuts I could find with nothing. Maybe because it's not an iPhone/iPad specific keyboard? – random May 02 '16 at 15:48
  • Indeed it would be nice to get one of the Apple Magic Keyboard to check it, I guess the power button [(or any Command+Eject etc. combination)](https://support.apple.com/en-qa/HT201236) should do the trick... – JBA May 02 '16 at 16:01
  • Testing with an Apple Magic Keyboard now and have gone through the list in the your link, nothing seems to be working. I have a keyboard that is specifically made for iPad at home I'm going to test at lunch. I saw some other iPad/iPhone specific keyboards online that seem to have other shortcuts available not on that list. I'll keep you posted! Thank you so much again for the insight, never once did it cross my mind they were piggy backing on the keyboard profile. nice find. – random May 02 '16 at 16:03
  • Thanks to keep us posted here. Not tested on a real one, but I don't see any other reason why their device acts as a keyboard :) – JBA May 02 '16 at 16:05
  • Another Hint : that could also be done with specific credentials with a [custom configuration profile](https://developer.apple.com/library/ios/featuredarticles/iPhoneConfigurationProfileRef/Introduction/Introduction.html) you have to install - from there you can play the maxInactivity that would tell the device to be idle for 0 minute before beeing idle – JBA May 02 '16 at 16:43
  • so there is a key on the keyboard for iPhone/iPad that does lock the screen. AND IT WORKS! I have no idea what the key code is sending via bluetooth though. I've been searching and searching, any ideas? I've played with `maxInactivity` but don't believe that to be the solution :-/ – random May 02 '16 at 17:30
  • Yes, the keyboard was the best bet. Thank you for having tested this. Now with a bluetooth signal sniffer you might be able to get the key-code - this is probably a simple ASCII code... – JBA May 02 '16 at 18:05
  • i've updated my question with recent findings. Will continue to post as I figure out more. – random May 02 '16 at 18:41
  • 1
    @random These findings are of great help for me. Doing the same stuff to lock the screen using a RPi(controller). How can i call the HID interrupt like you suggested programatically. I'm using python and js at controller side ? Any suggestions would be of great help ! – swathy krishnan Mar 22 '17 at 10:42
  • @random Can you be little more specific on which key combination you used to lock the phone ? – swathy krishnan Mar 22 '17 at 12:19
  • @swathykrishnan was never able to get it fully working – random Mar 24 '17 at 17:59
  • @random Ok. But still do you know the keystroke pair to lock ! – swathy krishnan Mar 27 '17 at 05:26
  • @swathykrishnan it seems like it's just a blank mouse click with zero'd out X,Y, Scroll and not a keystroke. Let me know if you're able to get it working! – random Mar 31 '17 at 18:12
  • Added an Edit to the answer: this power down (and up) is commanded by a "media-type" key, but this is still a common key, not a tricky-apple-specific-signal. So this is very easy to trigger the power up/down as long as you can send the HID command you want. – JBA Apr 01 '17 at 14:04
  • @JBA Thanks for your help. But still it's not working. – swathy krishnan Apr 03 '17 at 09:41
  • @random Zero'd out thing are release key event which occurs after press key event. So better you please sniff and share Press event. – ABHIJITH KINI Apr 15 '17 at 14:27
  • @JBA Can you please give an idea on HID Descriptor to set up for this purpose ? – ABHIJITH KINI Apr 20 '17 at 10:29
  • After 3 long painful months of hardcore development in things I had no clue about until starting, I can confirm that I have successfully accomplished everything in this discussion about a peripheral device doing these tasks. – GoreDefex Jul 17 '17 at 16:00
4

After contacting Apple Developer Technical Support there is no supported way to achieve this functionality without using private API's. Use of these will cause for your app to be rejected.

My guess is that CellControl was able to make it through review because the only way that they make use of this feature is if you have their hardware installed in your vehicle, device paired with it, and begin driving. My guess is that during app review, Apple did not buy one of their devices and actually test it. Although I've always been under the impression that they scan you binaries to check for undocumented API use but that seems to be wrong.

The other possibility as @Chris mentioned is they could have had an arrangement with Apple before starting development. While this seems unlikely, it is possible.

Here are some excerpts from Apple Developer Technical Support:

Thank you for contacting Apple Developer Technical Support (DTS). Our engineers have reviewed your request and have concluded that there is no supported way to achieve the desired functionality given the currently shipping system configurations.


Hello,

Developer Technical Support is not in a position to reverse engineer other developer's software on your behalf. Apps that are doing seemingly-impossible things generally fall into one of two categories:

  • they're breaking the rules and App Review hasn't caught them yet (A)

  • their marketing material is being economical with the truth (B)

I can say that there is no supported way lock the device from your iOS app.


Hope this helps someone in the future.

random
  • 8,568
  • 12
  • 50
  • 85
  • Correct, assuming you are using internal application logic to accomplish this task... This IS however in the realm of possibility when you explore the above solutions mentioned. – GoreDefex Jul 17 '17 at 16:01
0

After a long time research, I found it is no way to lock the screen programmatically. But I found there two ways to think about: 1. Jailbreak the iOS device; 2. Use MDM technique to achieve it. But it will be a huge work. We need to build a server to handle the communication with the iOS devices.

Nan
  • 1
0

With iOS 16.4 there is an easier way to achieve this by calling a shortcut that locks the device.

It's not perfect because shortcuts app open briefly, and looks like a workaround but at least, it exists:

lock iOS device programmatically

Clement M
  • 709
  • 6
  • 14