1

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?

Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
VSN
  • 2,361
  • 22
  • 30
  • check this - http://stackoverflow.com/questions/10319275/what-is-a-long-term-method-i-can-use-to-uniquely-identify-an-ios-device – rishi Jul 18 '14 at 15:56
  • 1
    User and group information on OS X is stored in Directory services, and the OpenDirectory.framework (https://developer.apple.com/library/mac/documentation/networking/Conceptual/Open_Directory/Introduction/Introduction.html) provides a programmatic interface. - And "dscl" is open source (http://opensource.apple.com/source/DSTools/DSTools-162/dscl/) so that should give you a starting point. – Martin R Jul 18 '14 at 16:23

2 Answers2

1

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;
}
Pat McG
  • 706
  • 5
  • 7
0

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".

VSN
  • 2,361
  • 22
  • 30