首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何删除登录信息的keyChain信息?

如何删除登录信息的keyChain信息?
EN

Stack Overflow用户
提问于 2011-03-03 02:27:31
回答 2查看 1.2K关注 0票数 0

这是我在登录时遇到的一些代码,但是你如何删除它,密钥链中的信息呢?

代码语言:javascript
复制
+ (User *)currentUserForSite:(NSURL *)aSiteURL {
    User *user = [[[self alloc] init] autorelease];
    user.siteURL = aSiteURL;
    [user loadCredentialsFromKeychain];
    return user;
}

- (BOOL)hasCredentials {    
    return (self.login != nil && self.password != nil);
}

- (BOOL)authenticate:(NSError **)error { 
    if (![self hasCredentials]) {
        return NO;
    }

    Session *session = [[[Session alloc] init] autorelease];
    session.login = self.login;
    session.password = self.password;

    return [session createRemoteWithResponse:error];
}

- (void)saveCredentialsToKeychain {
    NSURLCredential *credentials = 
    [NSURLCredential credentialWithUser:self.login
                               password:self.password
                            persistence:NSURLCredentialPersistencePermanent];

    [[NSURLCredentialStorage sharedCredentialStorage]   
     setCredential:credentials forProtectionSpace:[self protectionSpace]];
}

#pragma mark -
#pragma mark Key-value observing

- (void)addObserver:(id)observer {
    [self addObserver:observer forKeyPath:kUserLoginKey options:NSKeyValueObservingOptionNew context:nil];
    [self addObserver:observer forKeyPath:kUserPasswordKey options:NSKeyValueObservingOptionNew context:nil];
}

- (void)removeObserver:(id)observer {
    [self removeObserver:observer forKeyPath:kUserLoginKey];
    [self removeObserver:observer forKeyPath:kUserPasswordKey];
}

#pragma mark -
#pragma mark Private methods

- (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;
    }
}

- (NSURLProtectionSpace *)protectionSpace {
    return [[[NSURLProtectionSpace alloc] initWithHost:[siteURL host]
                                                  port:[[siteURL port] intValue]
                                              protocol:[siteURL scheme]
                                                 realm:@"Web Password"
                                  authenticationMethod:NSURLAuthenticationMethodDefault] autorelease];
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-26 00:40:36

我不是专家,但我不认为您可以删除存储在NSURLCredentialPersistencePermanent中的凭据。我只需要将凭据更新为空字符串- @"“

票数 2
EN

Stack Overflow用户

发布于 2015-05-04 09:16:51

我看到这个问题已经很老了,但对于今天关注的人来说,有一种方法可以删除凭据:

代码语言:javascript
复制
[[NSURLCredentialStorage sharedCredentialStorage] removeCredential:credential_object forProtectionSpace:protection_space];

参考:https://coderwall.com/p/kjb3lw/storing-password-in-keychain-the-smart-way

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5171732

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档