Get all Users on OS X

亡梦爱人 提交于 2019-11-27 23:23:14

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
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!