ios - AFNetworking with NSURLCredential -



ios - AFNetworking with NSURLCredential -

i have 3 methods, userlogin, login , logoutbuttonpressed:

userlogin: using afnetworking connect windows authenticated url using nsurlcredential:

-(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 beingness 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: prepare 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 within 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.

i tried clearing authorizationheader , cancelingalloperations , still nothing, still able login wrong or no creds after logout:

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

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 create sure have random number @ end of urls calling.

ios objective-c afnetworking nsurlcredential

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -