objective-c clear NSURLCnetworkingential – NSURLCnetworkingentialPersistenceForSession

Estoy usando NSURLCnetworkingentials en este método:

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

Y ahora estoy buscando borrar los NSURLCnetworkingentials en un método de cierre de session así:

 -(void)logout { //Clear NSURLCnetworkingentialPersistenceForSession } 

¿Cómo iba a hacer esto? ¿Debo restablecer los NSURLCnetworkingentials o está allí para borrarlos?

ACTUALIZAR

Encontré esta solución:

 NSURLCnetworkingentialStorage *cnetworkingentialStorage = [NSURLCnetworkingentialStorage shanetworkingCnetworkingentialStorage]; NSDictionary *cnetworkingentialsDicationary = [cnetworkingentialStorage allCnetworkingentials]; for (NSURLProtectionSpace *space in [cnetworkingentialsDicationary allKeys]) { NSDictionary *spaceDictionary = [cnetworkingentialsDicationary objectForKey:space]; for (id userName in [spaceDictionary allKeys]) { NSURLCnetworkingential *cnetworkingential = [spaceDictionary objectForKey:userName]; [cnetworkingentialStorage removeCnetworkingential:cnetworkingential forProtectionSpace:space]; } } 

Desde aquí: al utilizar NSURLCnetworkingentialPersistenceForSession mientras se solicita y, a continuación, borrar las cnetworkingenciales al cerrar session, persisten los cerdentials

Es esta la mejor manera? Solo tengo 1 cnetworkingencial almacenada. También este código me da una advertencia en esta línea:

 NSURLCnetworkingential *cnetworkingential = [spaceDictionary objectForKey:userName]; 

y aquí está la advertencia … ¿Cómo elimino esta advertencia?

 /Users/jsuske/Documents/SSiPad(Device Only)ios7/SchedulingiPadApplication/ViewControllers/LHLoginController.m:496:73: Local declaration of 'userName' hides instance variable 

ACTUALIZAR

De acuerdo, entonces tengo 3 methods: UserLogin, Login y LogoutButtonPressed

UserLogin: estoy usando AFNetworking para conectarse a una URL autenticada de Windows utilizando NSURLCnetworkingential como se muestra arriba:

 -(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ó.

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.

LogoutButtonPressed está eliminando las cnetworkingenciales de NSURLCnetworkingentialStorage mientras itera sobre las mismas cnetworkingenciales: NSEnumerator no admite eso. ¿Estás viendo el post "cnetworking to be removed" en el logging?

Podría probar el método _resetCnetworkingentials desde https://developer.apple.com/library/ios/samplecode/AdvancedURLConnections/Listings/CnetworkingentialsController_m.html .

Independientemente de si eso marca una diferencia, sería interesante examinar el resultado, tanto de antes como después de la llamada resetCnetworkingentials de _dumpCnetworkingentials, o mejor aún hacer el volcado en su método de inicio de session, y ver si hay un cambio inmediatamente después del reinicio y luego 2-3 minutos más tarde.

¿Se borra el text self.passwordTextField.text? En el caso de no cnetworkingenciales donde se registra, NSURLCnetworkingential cnetworkingentialWithUser: da cero?