Get all Users on OS X
So I want to implement Parental Controls per user in my app, but I need a way of getting all Users and add them to an NSTableView. These users should be the same displayed by the Login Window, excluding the Other... one and system users.
Any ideas on how to do this? Also开发者_StackOverflow中文版, I want to be able to get the selection on that table view and of course change the settings displayed according to that.
Here's how I do it:
#import <CoreServices/CoreServices.h>
#import <Collaboration/Collaboration.h>
CSIdentityAuthorityRef defaultAuthority = CSGetLocalIdentityAuthority();
CSIdentityClass identityClass = kCSIdentityClassUser;
CSIdentityQueryRef query = CSIdentityQueryCreate(NULL, identityClass, defaultAuthority);
CFErrorRef error = NULL;
CSIdentityQueryExecute(query, 0, &error);
CFArrayRef results = CSIdentityQueryCopyResults(query);
int numResults = CFArrayGetCount(results);
NSMutableArray * users = [NSMutableArray array];
for (int i = 0; i < numResults; ++i) {
CSIdentityRef identity = (CSIdentityRef)CFArrayGetValueAtIndex(results, i);
CBIdentity * identityObject = [CBIdentity identityWithCSIdentity:identity];
[users addObject:identityObject];
}
CFRelease(results);
CFRelease(query);
//users contains a list of known Aqua-style users.
The CBIdentity
objects are much more convenient to use than the CSIdentityRef
objects, but they do require importing the Collaboration framework.
From the command line, you can run
dscl localhost -list /Local/Default/Users
There are a lot of users that start with the underscore character that can be ignored by you app. This command can of course be run from within cocoa and the output read, but it can also be accessed more directly.
You can also use an Apple framework, but it is probably easier to use a wrapper. I can't find a very recent one right now, but search for something like this:
http://www.martinkahr.com/2006/10/15/cocoa-directory-services-wrapper/index.html
Here is a Swift version, note that in Xcode 7.2.1 " 'init(CSIdentity:)' is unavailable in Swift: CSIdentity is not available in Swift."
Instead we can use :
CBIdentity(uniqueIdentifier uuid: NSUUID, authority: CBIdentityAuthority)
func getSystemUsers()->[CBIdentity]{
let defaultAuthority = CSGetLocalIdentityAuthority().takeUnretainedValue()
let identityClass = kCSIdentityClassUser
let query = CSIdentityQueryCreate(nil, identityClass, defaultAuthority).takeRetainedValue()
var error : Unmanaged<CFErrorRef>? = nil
CSIdentityQueryExecute(query, 0, &error)
let results = CSIdentityQueryCopyResults(query).takeRetainedValue()
let resultsCount = CFArrayGetCount(results)
var allUsersArray = [CBIdentity]()
for idx in 0...resultsCount-1 {
let identity = unsafeBitCast(CFArrayGetValueAtIndex(results,idx),CSIdentityRef.self)
let uuidString = CFUUIDCreateString(nil, CSIdentityGetUUID(identity).takeUnretainedValue())
if let uuidNS = NSUUID(UUIDString: uuidString as String), let identityObject = CBIdentity(uniqueIdentifier: uuidNS, authority: CBIdentityAuthority.defaultIdentityAuthority()){
allUsersArray.append(identityObject)
}
}
return allUsersArray
}
精彩评论