2012-12-19 36 views
1

我正在嘗試使用此api SecKeychainFindGenericPassword()獲取機場網絡密碼。 但我總是得到itemnotfound錯誤。我不確定要在API中輸入帳戶名稱和服務名稱。我添加了代碼片段來顯示我在做什麼。任何幫助,將不勝感激 。 感謝使用SecKeychainFindGenericPassword獲取機場網絡密碼

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

回答

1

您有服務和帳戶交換。該服務應該是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); 
0

服務名稱是要獲取密碼和帳戶名稱服務的名稱是用戶的帳戶名(您可以通過調用NSUserName得到它() ,但要小心,這將返回一個NSString *,並且此API用C語言編寫,因此它期望有一個const char *)。

正如我所說的,因爲鑰匙串API是用C寫的,我強烈建議你使用一個包裝庫做的工作,如果你不舒適與普通C.我建議你這一個: https://github.com/samsoffes/sskeychain