Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

ios - Force lock screen

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...