gpt4 book ai didi

objective-c - SecKeychainFindInternetPassword 不工作

转载 作者:搜寻专家 更新时间:2023-10-30 20:20:14 25 4
gpt4 key购买 nike

我在存储和读取钥匙串(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com