json - How to make login page in iphone using webservices -
i'm working on app asks user login access of informations. have login.jsp file store on server , usernames , passwords on database.
on app have 2 uitextfields 1 username , 1 password. understand can use post method pass on input web server , check whether username , password match load rest of data. having trouble, can me it, how pass on inputs uitextfield web service run login.jsp script? in advance.
// jdurlconnection.h
typedef void (^successblock)(nsdictionary *dict); typedef void (^errorblock)(nsstring *error); @interface jdurlconnection : nsobject +(void) urlrequestwithurl:(nsstring *)urlstr usingmethod:(nsstring *)verb andparameters:(nsdictionary *) param completionblock:(successblock)completed errorblock:(errorblock)errored; @end
// jdurlconnection.m
+ (void) showmessagestring:(nsstring *)message{ uialertview * alert = [[uialertview alloc]initwithtitle:@"app name!" message:message delegate:nil cancelbuttontitle:@"ok" otherbuttontitles: nil]; [alert show]; } #pragma mark - requestmethods + (nsmutableurlrequest *) urlrequestfrom:(nsurl *)url verb:(nsstring *)verb parameters:(nsdictionary*)parameters{ nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url cachepolicy:nsurlrequestreloadignoringlocalandremotecachedata timeoutinterval:30]; [request sethttpmethod:verb]; if (parameters != nil) { nsdata *jsondata = [[nsstring stringwithformat:@"data=%@",[parameters jsonrepresentation]] datausingencoding:nsutf8stringencoding]; [request sethttpbody:jsondata]; } return request; } +(void) urlrequestwithurl:(nsstring *)urlstr usingmethod:(nsstring *)verb andparameters:(nsdictionary *) param completionblock:(successblock)completed errorblock:(errorblock)errored{ // [svprogresshud showwithmasktype:svprogresshudmasktypeclear]; nsurl *url = [nsurl urlwithstring:urlstr]; nsmutableurlrequest *request = [self urlrequestfrom:url verb:verb parameters:param]; nsoperationqueue *queue = [nsoperationqueue mainqueue]; [nsurlconnection sendasynchronousrequest:request queue:queue completionhandler:^(nsurlresponse *response, nsdata *data, nserror *error) { // [svprogresshud dismiss]; if ([data length] > 0 && error == nil){ id responsedata = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutableleaves error:nil]; nslog(@"%@",responsedata); nsdictionary *responsedict = nil; if ([responsedata iskindofclass:[nsdictionary class]]) responsedict = (nsdictionary *)responsedata; else if ([responsedata iskindofclass:[nsarray class]]) responsedict = [nsdictionary dictionarywithobject:responsedata forkey:@"result"]; else responsedict = [nsdictionary dictionarywithobject:@"" forkey:@"error"]; completed(responsedict); } else if ([data length] == 0 && error == nil){ [self performselectoronmainthread:@selector(showmessagestring:) withobject:@"error! unable receive data server." waituntildone:yes]; errored(@"error! unable receive data server."); } else if (error != nil){ [self performselectoronmainthread:@selector(showmessagestring:) withobject:[error localizeddescription] waituntildone:yes]; errored([error localizeddescription]); } }]; }
implement as:
nsarray *keys = [nsarray arraywithobjects:@"username",@"password", nil]; nsarray *values = [nsarray arraywithobjects:self.tfusername.text,self.tfuserpass.text,nil]; nsdictionary *param = [nsdictionary dictionarywithobjects:values forkeys:keys]; [jdurlconnection urlrequestwithurl:[nsstring stringwithformat:@"http://yourweburl/api/user-login"] usingmethod:@"post" andparameters:param completionblock:^(nsdictionary *dict) { if ([dict objectforkey:@"result"]) { nslog(@"%@",dict); } } errorblock:^(nsstring *error) { nslog(@"%@",error); // errored(error); }];
Comments
Post a Comment