Friday 15 July 2011

ios - xcode using json and POST to validate a username and password -



ios - xcode using json and POST to validate a username and password -

i new using post, , have written code takes username , password entered in , sends via post validation. if valid, homecoming true, if not homecoming false. think code works part, there must missing homecoming info because not output true or false. here code:

- (ibaction)btnlogin:(id)sender; { //getting username , password , putting string nsstring *post =[[nsstring alloc] initwithformat:@"username= %@ &password =%@",[self.lblusername text],[self.lblpassword text]]; //output string nslog(@"postdata: %@",post); //setting url post nsurl *url=[nsurl urlwithstring:@"myurl.php"]; //converts string info can used post nsdata *postdata = [post datausingencoding:nsasciistringencoding allowlossyconversion:yes]; //get length of string nsstring *postlength = [nsstring stringwithformat:@"%lu", (unsigned long)[postdata length]]; //formatting url nsmutableurlrequest *request = [[nsmutableurlrequest alloc] init] ; [request seturl:url]; [request sethttpmethod:@"post"]; [request setvalue:postlength forhttpheaderfield:@"content-length"]; [request setvalue:@"application/json" forhttpheaderfield:@"accept"]; [request setvalue:@"application/x-www-form-urlencoded" forhttpheaderfield:@"content-type"]; [request sethttpbody:postdata]; nsurlresponse *requestresponse; nsdata *requesthandler = [nsurlconnection sendsynchronousrequest:request returningresponse:&requestresponse error:nil]; nsstring *requestreply = [[nsstring alloc] initwithbytes:[requesthandler bytes] length:[requesthandler length] encoding:nsasciistringencoding]; nslog(@"requestreply: %@", requestreply);

}

i not sure part missing or wrong. runs fine, returns blank when test out.

this method

//create request nsmutableurlrequest *request = [[nsmutableurlrequest alloc] initwithurl:[nsurl urlwithstring:@"myurl.php"]]; // create method "get" or "post" [request sethttpmethod:@"post"]; //pass string server nsstring *userupdate =[nsstring stringwithformat:@"username=%@&password=%@",[self.lblusername text],[self.lblpassword text],nil]; //check value passed nslog(@"the info details =%@", userupdate); //convert string info nsdata *data1 = [userupdate datausingencoding:nsutf8stringencoding]; //apply info body [request sethttpbody:data1]; //create response , error nserror *err; nsurlresponse *response; nsdata *responsedata = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&err]; nsstring *ressrt = [[nsstring alloc]initwithdata:responsedata encoding:nsasciistringencoding]; //this response nslog(@"got response==%@", ressrt); if(ressrt) { nslog(@"got response"); } else { nslog(@"faield connect"); }

ios json xcode post

No comments:

Post a Comment