0

按照我用来获取存储在自定义钥匙串中的密码的一段代码

-(OSStatus *)getPasswordFromKeyChain:(NSString *)username{
OSStatus status;

[self createKeyChainIfNotExists];

const char *cService_name = "Mac Google Analytics App";
UInt32 service_length = strlen(cService_name);

const char *cUser_name = [username cStringUsingEncoding:NSUTF8StringEncoding];
UInt32 username_length = strlen(cUser_name);

void *passwordData = nil; 
SecKeychainItemRef itemRef = nil;
UInt32 passwordLength = nil;

status = SecKeychainFindGenericPassword(
                                        mSecKeychainRef,            // default keychain
                                        service_length,  // length of service name
                                        cService_name,    // service name
                                        username_length,// length of account name
                                        cUser_name,    // account name
                                        &passwordLength,  // length of password
                                        passwordData,        // pointer to password data
                                        NULL             // the item reference
                                        );

return status;
}

我想知道这段代码有什么问题,因为它永远不会来自这个调用:

SecKeychainFindGenericPassword

它指向此处显示的错误。

4

1 回答 1

0

为默认钥匙串传递 null,您需要一个指向 passwordData 的指针。这至少应该让您返回状态,以判断字符串对于您的服务和帐户是否正确。

    status = SecKeychainFindGenericPassword(NULL,            // default keychain
                                            service_length,  // length of service name
                                            cService_name,    // service name
                                            username_length,// length of account name
                                            cUser_name,    // account name
                                            &passwordLength,  // length of password
                                            &passwordData,        // pointer to password data
                                            NULL             // the item reference
                                            );
于 2012-11-16T17:10:00.433 回答