Очистка credentialsForProtectionSpace для установки только одного значения ключа. NSURLCredentialStorage
Я изучаю NSURLCredentialStorage и у меня возникла ситуация. Когда я разрабатывал свое приложение, мне пришлось хранить два имени пользователя с паролями, используя следующий код. У меня проблема в том, что нет необходимости хранить два набора комбинаций uname / pword. Итак, мой вопрос, как я могу сбросить память и начать все заново?
Вот как я загружаю учетные данные: Помните, что я использую пример загрузки из ObjectiveResource, и он захватывает объект с индексом 0. Я хочу, чтобы была только 1 пара объектов.
- (void)loadCredentialsFromKeychain {
NSDictionary *credentialInfo = [[NSURLCredentialStorage sharedCredentialStorage] credentialsForProtectionSpace:[self protectionSpace]];
// Assumes there's only one set of credentials, and since we
// don't have the username key in hand, we pull the first key.
NSArray *keys = [credentialInfo allKeys];
if ([keys count] > 0) {
NSString *userNameKey = [[credentialInfo allKeys] objectAtIndex:0];
NSURLCredential *credential = [credentialInfo valueForKey:userNameKey];
self.login = credential.user;
self.password = credential.password;
}
}
1 ответ
Решение
reset credential
с помощью NSURLCredentialStorage
removeCredential:forProtectionSpace
:
// reset the credentials cache...
NSDictionary *credentialsDict = [[NSURLCredentialStorage sharedCredentialStorage] allCredentials];
if ([credentialsDict count] > 0) {
// the credentialsDict has NSURLProtectionSpace objs as keys and dicts of userName => NSURLCredential
NSEnumerator *protectionSpaceEnumerator = [credentialsDict keyEnumerator];
id urlProtectionSpace;
// iterate over all NSURLProtectionSpaces
while (urlProtectionSpace = [protectionSpaceEnumerator nextObject]) {
NSEnumerator *userNameEnumerator = [[credentialsDict objectForKey:urlProtectionSpace] keyEnumerator];
id userName;
// iterate over all usernames for this protectionspace, which are the keys for the actual NSURLCredentials
while (userName = [userNameEnumerator nextObject]) {
NSURLCredential *cred = [[credentialsDict objectForKey:urlProtectionSpace] objectForKey:userName];
NSLog(@"cred to be removed: %@", cred);
[[NSURLCredentialStorage sharedCredentialStorage] removeCredential:cred forProtectionSpace:urlProtectionSpace];
}
}
}
Перейдите по этой ссылке.