Get all Users on OS X

后端 未结 3 1705
我在风中等你
我在风中等你 2020-12-06 03:54

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 b

相关标签:
3条回答
  • 2020-12-06 04:13

    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
        }
    
    0 讨论(0)
  • 2020-12-06 04:15

    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

    0 讨论(0)
  • 2020-12-06 04:28

    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.

    0 讨论(0)
提交回复
热议问题