VSN
VSN

Reputation: 2361

Get "GeneratedUID" programmatically in COCOA

I'm trying to get system GeneratedUID programmetically, i'm able to get the UID from command line using "dscl . -search /Users GeneratedUID 00052AE8-5000-6000-9007-666F6B666A66"

Can anyone help how to get this GeneratedUID programmetically?

Upvotes: 1

Views: 557

Answers (2)

Pat McG
Pat McG

Reputation: 706

This worked for me. It returns the first GeneratedUID that it finds for the current user, or nil if none are found.

There are lots of errors to handle, making the logic more complicated than it really should be.

-(NSString*) getGeneratedUID {

    NSString* retval = nil;
    ODSession *mySession = [ODSession defaultSession];
    NSError *err;
    ODNode *myNode = [ODNode nodeWithSession:mySession type:kODNodeTypeAuthentication error:&err];
    NSString* username = NSUserName();

    ODQuery *myQuery = [ODQuery  queryWithNode: myNode
                                forRecordTypes: kODRecordTypeUsers
                                     attribute: kODAttributeTypeRecordName
                                     matchType: kODMatchEqualTo
                                   queryValues: username
                              returnAttributes: kODAttributeTypeStandardOnly
                                maximumResults: 0
                                         error: &err];

    if (err) {
        NSLog(@"Error creating current user query: %@", [err localizedDescription]);
    }
    else {
        NSArray* queryResults = [myQuery resultsAllowingPartial:NO error:&err];

        if (err) {
            NSLog(@"Error executing current user query: %@", [err localizedDescription]);
        }
        else {
            if ( [queryResults count] > 0 ) {

                ODRecord* firstQueryResult = [queryResults objectAtIndex:0];
                NSArray* uidValues = [firstQueryResult valuesForAttribute:@kDS1AttrGeneratedUID error:&err];

                if (err) {
                    NSLog(@"Error getting GeneratedUID attribute for current user: %@", [err localizedDescription]);
                }
                else {
                    if ([uidValues count] > 0) {
                        retval = (NSString*) [uidValues objectAtIndex:0];
                    }
                    else {
                        NSLog(@"No GeneratedUID values for current username: %@", username);
                    }
                }
            }
            else {
                NSLog(@"No results from current user query for username: %@", username);
            }
        }
    }

    return retval;
}

Upvotes: 1

VSN
VSN

Reputation: 2361

just use the following code to retrive UID.... import the SecurityConfiguration.framework before you add the code..

NSString* GetConsoleUser(uid_t* p_uid, gid_t* p_gid) {
  NSString* name = (NSString*)CFBridgingRelease(SCDynamicStoreCopyConsoleUser(
                                                          NULL, p_uid, p_gid));

  return name ;
} 

you have to play around the string to get the "UID".

Upvotes: 0

Related Questions