Created
April 26, 2016 07:43
-
-
Save ftiff/a962e0a29b276f6c20bc22750168f03c to your computer and use it in GitHub Desktop.
Check who is the current user (uses undocumented function from CoreGraphics)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import CoreGraphics | |
@_silgen_name("CGSSessionCopyAllSessionProperties") private func CGSSessionCopyAllSessionProperties() -> CFArray? | |
func getCurrentUser() -> String? { | |
if let sessions: NSArray = CGSSessionCopyAllSessionProperties() { | |
for session in sessions { | |
let dict = session as! Dictionary<String, AnyObject> | |
if dict["kCGSSessionOnConsoleKey"] as! Bool { | |
return dict["kCGSSessionUserNameKey"] as? String | |
} | |
} | |
} | |
return nil | |
} | |
if let currentUser = getCurrentUser() { | |
print(currentUser) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment