4
我在Keychain Services編程指南中的Apple示例代碼後面的鑰匙串中存儲了通用密碼。從鑰匙串中檢索存儲的密碼在XCode外部失敗
只要我在Xcode的調試模式下運行應用程序,一切正常。但是,當我存檔並導出應用程序時,它仍會存儲密碼(在Keychain Access中可見),但無法檢索它們。
鑰匙串不斷返回errSecAuthFailed(-25293)。這種情況發生在Mountain Lion上,但不在Snow Leopard上。我的應用程序是代碼簽名和沙箱。對我來說,似乎在檢索密碼時,鑰匙串不會將該應用程序識別爲與存儲密碼的應用程序相同,因爲當我將密碼設置爲可由任何應用程序訪問時,它也可以很好地工作。
我使用下面的代碼:
+ (NSString*) retrievePasswordFromKeychainWithKey: (NSString*) theKey {
SecKeychainUnlock(NULL, 0, NULL, FALSE);
const char* userNameUTF8 = [NSUserName() UTF8String];
uint32_t userNameLength = (uint32_t)strlen(userNameUTF8);
uint32_t serviceNameLength = (uint32_t)strlen([theKey UTF8String]);
uint32_t pwLength = 0;
void* pwBuffer = nil;
SecKeychainItemRef itemRef = nil;
OSStatus status1 = SecKeychainFindGenericPassword (NULL, serviceNameLength, serviceNameUTF8, userNameLength, userNameUTF8, &pwLength, &pwBuffer, &itemRef);
if (status1 == noErr) {
NSData* pwData = [NSData dataWithBytes:pwBuffer length:pwLength];
SecKeychainItemFreeContent (NULL, //No attribute data to release
pwBuffer //Release data buffer allocated by SecKeychainFindGenericPassword
);
return [NSString stringWithCString:[pwData bytes] encoding:NSUTF8StringEncoding];
}
//status1 is always -25293
return nil;
}
我在10.8上遇到同樣的問題,非常有趣。我計劃在MacApp商店銷售我的應用程序,這是我留下的一個bug。 – banderson623 2012-08-13 20:42:14
這已在10.8.1中默默解決。 – codingFriend1 2012-08-24 07:19:51