title: Force lock screen
body: <p>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: </p>
  
  <pre><code>func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -&gt; 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
  }
  </code></pre>
  
  <p>However it doesn't seem to work. Are there any other alternatives? There is app on the market called <a href="https://itunes.apple.com/us/app/cellcontrol/id661169580?mt=8" rel="nofollow noreferrer">CellControl</a> that does this so I know it's possible, just can't seem to figure out how. </p>
  
  <p><em>I've also tried in obj-c taken from <a href="https://stackoverflow.com/questions/7316521/auto-lock-iphone-programmatically">this answer</a></em></p>
  
  <p>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. </p>
  
  <p><a href="https://i.stack.imgur.com/sv6fc.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sv6fc.gif" alt="enter image description here"></a></p>
  
  <p>I've also seen using private frameworks which would most definitely call for rejection: </p>
  
  <pre><code>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);
    //...
  }
  </code></pre>
  
  <p>When first launching the app they ask for permission to: </p>
  
  <ul>
  <li>Make data available to bluetooth devices even when not using the app</li>
  <li>Send push notifications</li>
  <li>Access contacts</li>
  <li>Access microphone</li>
  <li>Use location even when not using the app</li>
  </ul>
  
  <p>I don't know if any of these frameworks would give you the ability to lock the screen but maybe?...</p>
  
  <hr>
  
  <p><strong>Quick update:</strong> </p>
  
  <p>After some more research and huge help from <a href="https://stackoverflow.com/users/1720332/jba">JBA</a> 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: </p>
  
  <p><a href="https://i.stack.imgur.com/tLP3k.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tLP3k.png" alt="enter image description here"></a></p>
  
  <p>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. </p>