Get all Users on OS X

后端 未结 3 1714
我在风中等你
我在风中等你 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:28

    Here's how I do it:

    #import 
    #import 
    
    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.

提交回复
热议问题