我正在尝试以编程方式获取系统GeneratedUID,我可以使用“ dscl。-search / Users GeneratedUID 00052AE8-5000-6000-9007-666F6B666A66”从命令行获取UID。

谁能帮助以编程方式获取此GeneratedUID?

最佳答案

这对我有用。它返回为当前用户找到的第一个GeneratedUID;如果未找到,则返回nil。

有很多错误要处理,这使逻辑变得比实际更复杂。

-(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;
}

关于macos - 在COCOA中以编程方式获取“GeneratedUID”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24829191/

10-10 18:27