我正在使用一段代码来获取存储在我的自定义Keychain中的密码
-(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
指向错误shown here。
答案 0 :(得分: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
);