Get all Users on OS X

浪尽此生 提交于 2019-11-26 21:27:16

问题


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, I want to be able to get the selection on that table view and of course change the settings displayed according to that.


回答1:


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.




回答2:


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




回答3:


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
    }


来源:https://stackoverflow.com/questions/3681895/get-all-users-on-os-x

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