Objective c 使用SecKeychainFindGenericPassword获取机场网络密码

Objective c 使用SecKeychainFindGenericPassword获取机场网络密码,objective-c,macos,wifi,keychain,Objective C,Macos,Wifi,Keychain,我正在尝试使用此api SecKeychainFindGenericPassword()获取机场网络密码。 但我总是发现itemnotfound错误。我不确定在API中传入什么帐户名和服务名。我添加了代码片段来显示我在做什么。任何帮助都将不胜感激。 谢谢 我使用的是osx 10.8服务名称是您要获取密码的服务名称,帐户名称是用户的帐户名称(您可以通过调用NSUserName()来获取它,但请注意,这将返回一个NSString*,此API是用C编写的,因此它需要一个const char*) 正如我

我正在尝试使用此api SecKeychainFindGenericPassword()获取机场网络密码。 但我总是发现itemnotfound错误。我不确定在API中传入什么帐户名和服务名。我添加了代码片段来显示我在做什么。任何帮助都将不胜感激。 谢谢


我使用的是osx 10.8

服务名称是您要获取密码的服务名称,帐户名称是用户的帐户名称(您可以通过调用NSUserName()来获取它,但请注意,这将返回一个NSString*,此API是用C编写的,因此它需要一个const char*)

正如我所说,由于KeyChain API是用C编写的,如果您不适合使用普通C,我强烈建议您使用包装器库来完成这项工作。我建议您使用以下方法:

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

 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);
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);