SecKeychainFindGenericPassword 不适用于 osx



遵循一段代码,我正在使用这段代码来获取存储在自定义钥匙串中的密码

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

它指向此处显示的错误。

为默认钥匙串传递 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
                                            );

最新更新