2012-08-08 29 views
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; 
} 

回答

4

OK,我刚刚了解到,这是在Mac OS中10.8.0一个开放的bug。使用开发人员ID签署的应用无法访问钥匙串中的数据。 我希望这将在10.8.1中得到解决...

解决方法是不要用开发人员ID对应用程序进行签名。 (我也读过Lion下构建的应用程序不受此bug影响,但我无法测试它)

+0

我在10.8上遇到同样的问题,非常有趣。我计划在MacApp商店销售我的应用程序,这是我留下的一个bug。 – banderson623 2012-08-13 20:42:14

+0

这已在10.8.1中默默解决。 – codingFriend1 2012-08-24 07:19:51

相关问题