ios - objective-c clear NSURLCredential - NSURLCredentialPersistenceForSession -
i using nsurlcredentials
in method:
-(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]; nsurlcredential *credential = [nsurlcredential credentialwithuser:user password:password persistence:nsurlcredentialpersistenceforsession]; [operation setcredential:credential]; [[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]; }
and looking clear nsurlcredentials in logout method so:
-(void)logout { //clear nsurlcredentialpersistenceforsession }
how go doing this? should reset nsurlcredentials or there away clear them ?
update
i found solution:
nsurlcredentialstorage *credentialstorage = [nsurlcredentialstorage sharedcredentialstorage]; nsdictionary *credentialsdicationary = [credentialstorage allcredentials]; (nsurlprotectionspace *space in [credentialsdicationary allkeys]) { nsdictionary *spacedictionary = [credentialsdicationary objectforkey:space]; (id username in [spacedictionary allkeys]) { nsurlcredential *credential = [spacedictionary objectforkey:username]; [credentialstorage removecredential:credential forprotectionspace:space]; } }
is best way? have 1 credential stored. code gives me warning on line:
nsurlcredential *credential = [spacedictionary objectforkey:username];
and here warning...how remove warning?
/users/jsuske/documents/ssipad(device only)ios7/schedulingipadapplication/viewcontrollers/lhlogincontroller.m:496:73: local declaration of 'username' hides instance variable
update
okay, have 3 methods:userlogin, login , logoutbuttonpressed
userlogin: using afnetworking connect windows authenticated url using nsurlcredential shown above:
-(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]; nsurlcredential *credential = [nsurlcredential credentialwithuser:user password:password persistence:nsurlcredentialpersistenceforsession]; [operation setcredential:credential]; [[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]; }
this method being called login method:
- (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; } }]; }
and trying clear session logoutbuttonpressed:
- (void)logoutbuttonpressed { //@todo: fix logout nsdictionary *credentialsdict = [[nsurlcredentialstorage sharedcredentialstorage] allcredentials]; if ([credentialsdict count] > 0) { nsenumerator *protectionspaceenumerator = [credentialsdict keyenumerator]; id urlprotectionspace; while (urlprotectionspace = [protectionspaceenumerator nextobject]) { nsenumerator *usernameenumerator = [[credentialsdict objectforkey:urlprotectionspace] keyenumerator]; id usernamecred; while (usernamecred = [usernameenumerator nextobject]) { nsurlcredential *cred = [[credentialsdict objectforkey:urlprotectionspace] objectforkey:usernamecred]; nslog(@"cred removed: %@", cred); [[nsurlcredentialstorage sharedcredentialstorage] removecredential:cred forprotectionspace:urlprotectionspace]; } } } }
i got code example: http://www.springenwerk.com/2008/11/i-am-currently-building-iphone.html
now problem having when trigger logout button , goto trigger login method no credentials can still login, if logout wait 2 - 3 minutes , login no credentials can't login. why behaving way, creds still saved. please help.
update
i have tried clear cache, cookies , creds inside logoutbuttonpressed:
nsurlcache *sharedcache = [nsurlcache sharedurlcache]; [sharedcache removeallcachedresponses]; nshttpcookiestorage *cookiestorage = [nshttpcookiestorage sharedhttpcookiestorage]; nsarray *cookies = [cookiestorage cookies]; id cookie; (cookie in cookies) { [cookiestorage deletecookie:cookie]; } nsdictionary *credentialsdict = [[nsurlcredentialstorage sharedcredentialstorage] allcredentials]; if ([credentialsdict count] > 0) { nsenumerator *protectionspaceenumerator = [credentialsdict keyenumerator]; id urlprotectionspace; while (urlprotectionspace = [protectionspaceenumerator nextobject]) { nsenumerator *usernameenumerator = [[credentialsdict objectforkey:urlprotectionspace] keyenumerator]; id usernamecreds; while (usernamecreds = [usernameenumerator nextobject]) { nsurlcredential *cred = [[credentialsdict objectforkey:urlprotectionspace] objectforkey:usernamecreds]; [[nsurlcredentialstorage sharedcredentialstorage] removecredential:cred forprotectionspace:urlprotectionspace]; } } }
and still did not work.
this issue can fixed adding random number end of 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]; nsurlcredential *credential = [nsurlcredential credentialwithuser:user password:password persistence:nsurlcredentialpersistenceforsession]; [operation setcredential:credential]; 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]; }
and make sure have random number @ end of urls calling.