AFNetworking con NSURLCnetworkingential

Tengo 3 methods, UserLogin, Login y LogoutButtonPressed:

UserLogin: estoy usando AFNetworking para conectarse a una URL autenticada de Windows utilizando NSURLCnetworkingential:

-(void)UserLogin:(NSString *)user andPassWordExists:(NSString *)password completionHandler:(void (^)(NSArray *resultsObject, NSError *error))completionHandler { NSURL *url = [NSURL URLWithString:kIP]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; NSURLCnetworkingential *cnetworkingential = [NSURLCnetworkingential cnetworkingentialWithUser:user password:password persistence:NSURLCnetworkingentialPersistenceForSession]; [operation setCnetworkingential:cnetworkingential]; [[NSOperationQueue mainQueue] addOperation:operation]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { if (completionHandler) { completionHandler(responseObject, nil); } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { if (completionHandler) { completionHandler(nil, error); } }]; [operation start]; } 

El método de inicio de session llama a este método:

 - (void)Login { NSString *rawString = [self.idTextField text]; NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet]; [self.idTextField setText:[rawString stringByTrimmingCharactersInSet:whitespace]]; [userName UserLogin:self.idTextField.text andPassWordExists:self.passwordTextField.text completionHandler:^(id responseObject, NSError *error) { if (responseObject) { [self.idTextField removeFromSuperview]; [self.passwordTextField removeFromSuperview]; [self.loginButton removeFromSuperview]; self.idTextField = nil; self.passwordTextField = nil; //self.loginButton = nil; [self CreateMenu]; [indicatorView stopAnimating]; [indicatorView removeFromSuperview]; indicatorView = nil; [loadingView removeFromSuperview]; loadingView = nil; }else{ [self CustomAlert:@"Sorry Login Failed, User and/or Passsword Incorrect"]; [indicatorView stopAnimating]; [indicatorView removeFromSuperview]; indicatorView = nil; [loadingView removeFromSuperview]; loadingView = nil; } }]; } 

Y estoy tratando de borrar mi session con el LogoutButtonPressed:

 - (void)LogoutButtonPressed { //@TODO: Fix Logout NSDictionary *cnetworkingentialsDict = [[NSURLCnetworkingentialStorage shanetworkingCnetworkingentialStorage] allCnetworkingentials]; if ([cnetworkingentialsDict count] > 0) { NSEnumerator *protectionSpaceEnumerator = [cnetworkingentialsDict keyEnumerator]; id urlProtectionSpace; while (urlProtectionSpace = [protectionSpaceEnumerator nextObject]) { NSEnumerator *userNameEnumerator = [[cnetworkingentialsDict objectForKey:urlProtectionSpace] keyEnumerator]; id userNameCnetworking; while (userNameCnetworking = [userNameEnumerator nextObject]) { NSURLCnetworkingential *cnetworking = [[cnetworkingentialsDict objectForKey:urlProtectionSpace] objectForKey:userNameCnetworking]; NSLog(@"cnetworking to be removed: %@", cnetworking); [[NSURLCnetworkingentialStorage shanetworkingCnetworkingentialStorage] removeCnetworkingential:cnetworking forProtectionSpace:urlProtectionSpace]; } } } } 

Recibí este código de este ejemplo: http://www.springenwerk.com/2008/11/i-am-currently-building-iphone.html

Ahora mi problema que estoy teniendo es que cuando desencadenar el button de cierre de session y luego activar el método de inicio de session SIN CREDENCIALES, todavía puedo iniciar session. Si cierre la session, espere 2 a 3 minutos e inicie session sin CREDENCIALES. No puedo iniciar session. ¿Por qué se comporta de esta manera, es casi como los cnetworkings todavía se guardan? Por favor ayuda.

ACTUALIZAR

He intentado borrar el caching, cookies y códigos dentro de mi LogoutButtonPressed:

 NSURLCache *shanetworkingCache = [NSURLCache shanetworkingURLCache]; [shanetworkingCache removeAllCachedResponses]; NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage shanetworkingHTTPCookieStorage]; NSArray *cookies = [cookieStorage cookies]; id cookie; for (cookie in cookies) { [cookieStorage deleteCookie:cookie]; } NSDictionary *cnetworkingentialsDict = [[NSURLCnetworkingentialStorage shanetworkingCnetworkingentialStorage] allCnetworkingentials]; if ([cnetworkingentialsDict count] > 0) { NSEnumerator *protectionSpaceEnumerator = [cnetworkingentialsDict keyEnumerator]; id urlProtectionSpace; while (urlProtectionSpace = [protectionSpaceEnumerator nextObject]) { NSEnumerator *userNameEnumerator = [[cnetworkingentialsDict objectForKey:urlProtectionSpace] keyEnumerator]; id userNameCnetworkings; while (userNameCnetworkings = [userNameEnumerator nextObject]) { NSURLCnetworkingential *cnetworking = [[cnetworkingentialsDict objectForKey:urlProtectionSpace] objectForKey:userNameCnetworkings]; [[NSURLCnetworkingentialStorage shanetworkingCnetworkingentialStorage] removeCnetworkingential:cnetworking forProtectionSpace:urlProtectionSpace]; } } } 

y todavía no funcionó.

También traté de borrar AuthorizationHeader y cancelar AllOperations y todavía nada, todavía puedo iniciar session con errores o sin cnetworkings después de cerrar session:

 NSURLCache *shanetworkingCache = [NSURLCache shanetworkingURLCache]; [shanetworkingCache removeAllCachedResponses]; NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage shanetworkingHTTPCookieStorage]; NSArray *cookies = [cookieStorage cookies]; id cookie; for (cookie in cookies) { [cookieStorage deleteCookie:cookie]; } NSDictionary *cnetworkingentialsDict = [[NSURLCnetworkingentialStorage shanetworkingCnetworkingentialStorage] allCnetworkingentials]; if ([cnetworkingentialsDict count] > 0) { NSEnumerator *protectionSpaceEnumerator = [cnetworkingentialsDict keyEnumerator]; id urlProtectionSpace; while (urlProtectionSpace = [protectionSpaceEnumerator nextObject]) { NSEnumerator *userNameEnumerator = [[cnetworkingentialsDict objectForKey:urlProtectionSpace] keyEnumerator]; id userNameCnetworkings; while (userNameCnetworkings = [userNameEnumerator nextObject]) { NSURLCnetworkingential *cnetworking = [[cnetworkingentialsDict objectForKey:urlProtectionSpace] objectForKey:userNameCnetworkings]; [[NSURLCnetworkingentialStorage shanetworkingCnetworkingentialStorage] removeCnetworkingential:cnetworking forProtectionSpace:urlProtectionSpace]; } } } NSURL *url = [NSURL URLWithString:kIP]; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; AFHTTPRequestSerializer <AFURLRequestSerialization> * requestSerializer = manager.requestSerializer; [requestSerializer clearAuthorizationHeader]; AFHTTPRequestOperationManager *httpClient = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:url]; [[httpClient operationQueue] cancelAllOperations]; 

Este problema se puede solucionar fácilmente agregando un número aleatorio al final de la URL:

 -(void)UserLogin:(NSString *)user andPassWordExists:(NSString *)password completionHandler:(void (^)(NSArray *resultsObject, NSError *error))completionHandler { NSInteger randomNumber = arc4random() % 999; NSString *requestURL = [NSString stringWithFormat:@"%@?cache=%ld",kIP,(long)randomNumber]; NSURL *url = [NSURL URLWithString:requestURL]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; NSURLCnetworkingential *cnetworkingential = [NSURLCnetworkingential cnetworkingentialWithUser:user password:password persistence:NSURLCnetworkingentialPersistenceForSession]; [operation setCnetworkingential:cnetworkingential]; operation.responseSerializer = [AFJSONResponseSerializer serializer]; [[NSOperationQueue mainQueue] addOperation:operation]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { if (completionHandler) { completionHandler(responseObject, nil); } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { if (completionHandler) { completionHandler(nil, error); } }]; [operation start]; } 

Y asegúrese de tener un número aleatorio al final de todas las URL a las que llama.