Skip to content

Instantly share code, notes, and snippets.

@ftiff
Created April 26, 2016 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ftiff/a962e0a29b276f6c20bc22750168f03c to your computer and use it in GitHub Desktop.
Save ftiff/a962e0a29b276f6c20bc22750168f03c to your computer and use it in GitHub Desktop.
Check who is the current user (uses undocumented function from CoreGraphics)
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