我正在学习关于NSURLCredentialStorage的知识,并且有一个情况。当我开发我的应用程序时,我碰巧使用以下代码存储了两个用户名和密码。我的问题是,没有必要存储两组uname/pword组合。因此,我的问题是,如何重置存储并重新开始?
下面是我加载凭据的方式:请注意,我使用的是load from ObjectiveResource示例,它在索引0处抓取对象。我希望只有一个对象对。
- (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;
}}
发布于 2012-12-04 12:21:29
使用NSURLCredentialStorage removeCredential:forProtectionSpace的reset credential:
// 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];
}
}
}请参阅this链接。
https://stackoverflow.com/questions/13694559
复制相似问题