ios - 使用 SSKeychain 存储在钥匙串(keychain)中的用户名是否总是出现在第 4 个索引中?

标签 ios iphone keychain

我正在使用 SSKeychain 将我的凭据存储在 iphone 中。但是当我尝试使用以下代码检索它时

NSArray *accountsArr = [SSKeychain accountsForService:@"User"];
    if(accountsArr)
    {
            NSDictionary *credentialsDict =accountsArr[0];
            NSArray *userNames = [credentialsDict allKeys];
            NSString *credential = (NSString *)credentialsDict[userNames[3]];
            self.usernameTxtFld.text = credential;
            credential = [SSKeychain passwordForService:@"User" account:self.usernameTxtFld.text];
            self.passwordTxTFld.text =  credential.length>0?credential:nil;
    }

我需要为存储的用户名指定 userNames[3] 这总是出现在索引 3 中还是应该为此使用其他方法?

最佳答案

不,因为字典键没有固有的顺序。 NSDictionary documentation在其对 allKeys 的讨论中明确说明了这一点。方法:

The order of the elements in the array is not defined.



拥有字典的意义在于您可以通过特定的键查找某些内容。在这种情况下,您应该使用 kSSKeychainAccountKey 获取帐户名称。 SSKeychain.h中定义的常量,如图:
NSArray *accountsArray = [SSKeychain accountsForService:@"User"];
if ([accountsArray count] > 0) {
    NSDictionary *credentialsDictionary = accountsArray[0];
    NSString *accountName = credentialsDictionary[kSSKeychainAccountKey];
    NSString *password = [SSKeychain passwordForService:@"User" account:accountName];
    self.passwordTextField.text = password;
}

关于ios - 使用 SSKeychain 存储在钥匙串(keychain)中的用户名是否总是出现在第 4 个索引中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19781797/

相关文章:

ios - 是否可以在项目中添加 .pch 文件?

iphone - Objective-C属性访问: unrecognized selector sent to instance

iphone - 让 modalViewController 出现的问题

ios - kSecAttrSynchronizable 需要 iCloud 钥匙串(keychain)吗?

ios - Keychain Services API 在 iphonesimulator 6.0 中因 errSecNotAvailable 而失败

ios - 将视频嵌入到 UIView 中并停止它

ios - UITextView 文本空间在调整大小时变小

iPhone 。自定义导航 Controller

iphone - UIView:不透明 vs. alpha vs. 不透明

iphone - kSecReturnPersistentRef 有什么作用?