作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在存储和读取钥匙串(keychain)时遇到问题。首先,我尝试在方法 SecKeychainFindInternetPassword 中使用像这样的“account_name”这样的字符来存储帐户名,然后运行。但现在我想在里面存储一个变量。但是如果我运行这段代码,Programm 找不到钥匙串(keychain)项。请帮我。(抱歉我的英语不好,我是德国学生)
-(void)StorePasswordKeychain:(void*)password :(UInt32)passwordLength
{
char *userString;
userString = (char *)[_username UTF8String];
SecKeychainAddInternetPassword(
NULL,
StrLength("myserver.com"),
"myserver.com",
0,
NULL,
StrLength(userString),
userString,
0,
nil,
0,
kSecProtocolTypeHTTPS,
kSecAuthenticationTypeHTMLForm,
passwordLength,
password,
NULL
);
}
-(OSStatus)GetPasswordKeychain:(void *)passwordData :(UInt32 *)passwordLength
{
OSStatus status;
char *userString;
userString = (char *)[_username UTF8String];
status = SecKeychainFindInternetPassword(
NULL,
StrLength("myserver.com"),
"myserver.com",
0,
NULL,
StrLength(userString),
userString,
0,
nil,
0,
kSecProtocolTypeHTTPS,
kSecAuthenticationTypeHTMLForm,
passwordLength,
passwordData,
NULL
);
return status;
}
最佳答案
两个建议.. 不要将 null 传递给 itemRef(最后一个参数)。然后您将有一个指向您要修改的钥匙串(keychain)的指针。
此外,您真的应该检查错误代码以查看您的添加功能是否有效。
OSStatus result = SecKeychainAddInternetPassword(
NULL,
StrLength("myserver.com"),
"myserver.com",
0,
NULL,
StrLength(userString),
userString,
0,
nil,
0,
kSecProtocolTypeHTTPS,
kSecAuthenticationTypeHTMLForm,
passwordLength,
password,
NULL
);
if(result != noErr){
NSLog(@"Error AddPassword result=:%d", result );
}
这是我的示例程序,代码与您提供的相同,运行良好。
int main(int argc, const char * argv[])
{
@autoreleasepool {
char *inputpassword = "topsecret";
UInt32 inputpassLength = strlen(inputpassword);
OSStatus status;
NSString *_username = @"account_name";
char *userString;
userString = (char *)[_username UTF8String];
status = SecKeychainAddInternetPassword(
NULL,
StrLength("myserver.com"),
"myserver.com",
0,
NULL,
StrLength(userString),
userString,
0,
nil,
0,
kSecProtocolTypeHTTPS,
kSecAuthenticationTypeHTMLForm,
inputpassLength,
inputpassword,
NULL
);
NSLog(@"Adding Status:%d", status);
UInt32 returnpasswordLength = 0;
char *passwordData;
status = SecKeychainFindInternetPassword(
NULL,
StrLength("myserver.com"),
"myserver.com",
0,
NULL,
StrLength(userString),
userString,
0,
nil,
0,
kSecProtocolTypeHTTPS,
kSecAuthenticationTypeHTMLForm,
&returnpasswordLength,
(void *)&passwordData,
NULL
);
NSLog(@"Retrieving status:%d", status);
NSLog(@"Password:%@", [[NSString alloc] initWithBytes:passwordData
length:returnpasswordLength
encoding:NSUTF8StringEncoding]);
}
return 0;
}
关于objective-c - SecKeychainFindInternetPassword 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13531073/
我在存储和读取钥匙串(keychain)时遇到问题。首先,我尝试在方法 SecKeychainFindInternetPassword 中使用像这样的“account_name”这样的字符来存储帐户名
我是一名优秀的程序员,十分优秀!