iphone - Save the username & password after login for the first time and not to ask again -
i developing 1 ios app, in need enter company-code , accesscode login. after login first time access code, cannot prefer ask accesscode again when open application. mean need save preferences.
and code here
- (ibaction)loginclicked:(id)sender { @try { if([[txtsecurecode text] isequaltostring:@""] || [[organizationcode text] isequaltostring:@""] ) { [self alertstatus:@"please enter access code" :@"login failed!":0]; } else { nsstring *post =[[nsstring alloc] initwithformat:@"txtsecurecode=%@ @&password=%@",[txtsecurecode text],[organizationcode text]]; nslog(@"postdata: %@",post); nsdata *postdata = [post datausingencoding:nsutf8stringencoding allowlossyconversion:yes]; nsurl *url = [nsurl urlwithstring:[nsstring stringwithformat:@"http://mycompany.com/accountservice/security/validateaccess?accesscode=%@&companycode=%@&type=1", txtsecurecode.text, organizationcode.text]]; [[ nsuserdefaults standarduserdefaults] setvalue:organizationcode forkey:@"organizationcode"]; [[ nsuserdefaults standarduserdefaults] setvalue:txtsecurecode forkey:@"txtsecurecode"]; [[nsuserdefaults standarduserdefaults] synchronize]; txtsecurecode = [[nsuserdefaults standarduserdefaults] objectforkey:@"txtsecurecode"]; organizationcode = [[nsuserdefaults standarduserdefaults] objectforkey:@"organizationcode"]; // nsstring *num = [[nsuserdefaults standarduserdefaults] stringforkey:@"txtsecurecode"]; nsstring *responsedata = [[nsstring alloc]initwithdata:[nsdata datawithcontentsofurl:url] encoding:nsutf8stringencoding]; if([responsedata isequaltostring:@""]){ [self alertstatus:@"please enter valid access code" :@"login failed !" :0]; } else { responsedata = [responsedata stringbyreplacingoccurrencesofstring:@" "" " withstring:@""]; responsedata = [responsedata stringbyreplacingoccurrencesofstring:@"\\" withstring:@""]; nsstring* encodedstring = [responsedata stringbyreplacingpercentescapesusingencoding: nsutf8stringencoding]; nslog(@"response ==> %@" ,encodedstring); uiwebview *webview; webview = [[uiwebview alloc] initwithframe:cgrectmake(0,0, 320, 470)]; webview.backgroundcolor = [uicolor clearcolor]; webview.opaque = no; [webview setdelegate:self]; nsstring* urltwo = [[encodedstring stringbyaddingpercentescapesusingencoding:nsutf8stringencoding] stringbyreplacingoccurrencesofstring:@"%22" withstring:@""]; nsurl *url2; if([urltwo hasprefix:@"http://"]){ url2 = [nsurl urlwithstring:urltwo]; }else{ url2 = [nsurl urlwithstring:[nsstring stringwithformat:@"http://%@" , urltwo]]; } nslog(@"url2:%@", url2); nsurlrequest *requestobj = [nsurlrequest requestwithurl:url2]; [webview loadrequest:requestobj]; [[self view] addsubview:webview]; } } } @catch (nsexception * e) { nslog(@"exception: %@", e); [self alertstatus:@"login failed." :@"login failed!" :0]; } } i had used nsuserdefaults method save login credentials not working. credentials not saving, again app asking credentials. can me in regard. in advance...
try this
when successfull login save in nsuserdefaults
nsuserdefaults *status=[nsuserdefaults standarduserdefaults]; [status setobject:@"1" forkey:@"login"]; next time check
nsuserdefaults *objuserdefaults = [nsuserdefaults standarduserdefaults]; nsstring *strsuccess=[objuserdefaults objectforkey:@"login"]; if ([strsuccess isequaltostring:@"1"]) { // push next view. } else { // open login screen } or next option, if care security, the keychain.
edit
.h file
nsuserdefaults *objuserdefaults; @property (nonatomic,retain) nsuserdefaults *objuserdefaults; .m file
@synthesize objuserdefaults; where first time login method write on response\
objuserdefaults = [nsuserdefaults standarduserdefaults] [objuserdefaults setobject:@"1" forkey:@"login"]; , when required login check nsstring *strsuccess=[objuserdefaults objectforkey:@"login"]; if ([strsuccess isequaltostring:@"1"]) { // push next view. } else { // open login screen }
Comments
Post a Comment