2

我正在尝试使用此 api SecKeychainFindGenericPassword() 获取机场网络密码。但我总是得到 itemnotfound 错误。我不确定在 API 中传递 Account name 和 service name 的内容。我添加了代码片段来显示我在做什么。任何帮助,将不胜感激 。谢谢

 OSStatus status1 ;

SecKeychainRef kychain = nil;
SecKeychainCopyDefault(&kychain);
status1 = SecKeychainFindGenericPassword (
                                          kychain,           // default keychain
                                          15,             // length of service name
                                          "AirPort Network",   // service name
                                          38,             // length of account name
                                          "com.apple.network.wlan.ssid.xxxxxxxx",   // account name
                                          passwordLength,  // length of password
                                          &passwordData,   // pointer to password data
                                          itemRef          // the item reference
                                          );
return (status1);

我正在使用 osx 10.8

4

2 回答 2

2

您已经交换了服务和帐户。服务应为 SSID,帐户名称应为“AirPort”。

SecKeychainRef keychain;
OSStatus err = SecKeychainOpen("/Library/Keychains/System.keychain", &keychain);

#define kServiceName "com.apple.network.wlan.ssid.Rackus"
#define kAccountName "AirPort"

UInt32 passwordLength = 0;
char* passwordData = nil;
UInt32 serviceNameLength = strlen(kServiceName);
UInt32 accountNameLength = strlen(kAccountName);
SecKeychainItemRef itemRef;

err = SecKeychainFindGenericPassword(keychain, serviceNameLength, kServiceName, accountNameLength, kAccountName, &passwordLength, (void**)&passwordData, &itemRef);
于 2013-10-25T23:13:17.607 回答
0

Service Name 是你要获取密码的服务名称,account name 是用户的账户名(你可以通过调用 NSUserName() 来获取,但是要注意,这会返回一个 NSString* 并且这个 API 是用 C 编写的,所以它需要一个 const char*)。

正如我所说,由于 KeyChain API 是用 C 编写的,我强烈建议您使用包装库来完成这项工作,如果您不习惯使用纯 C。我建议您使用这个: https://github。 com/samsoffes/sskeychain

于 2013-03-06T21:06:48.403 回答