Lock macbook screen from a sandboxed app
Asked Answered
H

1

6

I'm building a mac app that I want to distribute in the mac app store. I need this app to have a lock screen feature.

I have 2 different approaches working, the problem is, as soon as I enable sandboxing for the app (which is required for the mac app store), neither of those approaches will work.

Do you know which entitlement I need to request? Or do you know of a third approach that will work with sandboxing?

Thanks

Approach 1, using CGSession (swift):

var arguments = ["-suspend"]
var task = NSTask()
task.arguments = arguments
task.launchPath = "/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession"
task.launch()

Approach 2, using IORequestIdle (swift):

var r = IORegistryEntryFromPath(kIOMasterPortDefault, "IOService:/IOResources/IODisplayWrangler") 
if (r > 0) {
  IORegistryEntrySetCFProperty(r, "IORequestIdle", kCFBooleanTrue)
  IOObjectRelease(r)
}
Heise answered 6/11, 2014 at 11:39 Comment(5)
I'm happy with an Objective-C solution by the wayHeise
What is the need for lock screen? Privacy? Data protection? Why lock the whole Mac and not only your application? To lock the whole mac ask the user to activate System Preferences > Security > "Require password... after sleep or screen saver" and set an active corner to enter sleep of screen / screensaver. I doubt that this can be done programmatically as it involves admin rights.Miscall
Because the goal is to lock the mac. Not the app. The goal is to offer a "lock button", not a tutorial on how to lock the mac (which we are already doing in the meantime) It can be done programmatically because we've already done it with the app out of Sandbox mode. The question is about how to do it in sandbox mode (to distribute the app)Heise
FYI the Keychain Access app can put a "lock screen" item in the menu bar at all times if you enable the corresponding option in its preferences.Transmute
Yes I know, thanks, that is what our tutorial is saying for nowHeise
M
0

Sorry to say it can't be done. the purpose of sandboxing is to prevent an app can take the whole computer.

You might try to get a temporary exception thru the channels documented in the sandboxing guide.

Miscall answered 19/12, 2014 at 18:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.