开发者

How do you remove the keyChain information for login info?

开发者 https://www.devze.com 2023-02-14 15:28 出处:网络
Here is some code I came accross for logging in but how would you delete it the information in the keychain?

Here is some code I came accross for logging in but how would you delete it the information in the keychain?

+ (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 coun开发者_如何学编程t] > 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];
}


I'm not an expert but I don't think you can delete credentials stored with NSURLCredentialPersistencePermanent. I would just update the credentials to empty strings - @""


I see this question is quite old, but for people looking today, there is a way to remove a credential:

[[NSURLCredentialStorage sharedCredentialStorage] removeCredential:credential_object forProtectionSpace:protection_space];

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

0

精彩评论

暂无评论...
验证码 换一张
取 消