Parsing Json to handle and direct a user to view controller. Objective C -


i have login screen , post username , password login page.

the webservice gives me 2 responses if login user details correct response of request.

{"value":1}

and if user details wrong request.

{"value":0}

i have been able parse json result give me log output of

value: 1 or value:0

i battling handle parsed json e.g

 //parse out json data nsdictionary* json = [nsjsonserialization jsonobjectwithdata:urldata //1                                                      options:kniloptions                                                        error:&error];  nsarray* definejsondata = [json objectforkey:@"value"]; //2  nslog(@"value: %@", definejsondata); //3  if ([[json objectforkey:@"value"] isequaltonumber:[nsnumber numberwithint:1]]) {      hud.customview = [[uiimageview alloc] initwithimage:[uiimage imagenamed:@"37x-checkmark.png"]];     hud.mode = mbprogresshudmodecustomview;     [hud hide:yes afterdelay:0];     [self performseguewithidentifier: @"introscreenview" sender:self]; }  else {     hud.customview = [[uiimageview alloc] initwithimage:[uiimage imagenamed:@"37x-checkmark.png"]];     hud.mode = mbprogresshudmodecustomview;     [hud hide:yes afterdelay:0];     uialertview *alert = [[uialertview alloc] initwithtitle:@"wrong credentials" message:@"please try login again" delegate:self cancelbuttontitle:@"ok" otherbuttontitles:nil, nil];     [alert show]; } 

here rest of code.

- (void)mytask {  if ([usernametextfield.text isequaltostring:@""] || [passwordtextfield.text isequaltostring:@""]) {     hud.customview = [[uiimageview alloc] initwithimage:[uiimage imagenamed:@"37x-checkmark.png"]];     hud.mode = mbprogresshudmodecustomview;     [hud hide:yes afterdelay:0];     uialertview *alert = [[uialertview alloc] initwithtitle:@"feilds missing" message:@"please fill field" delegate:self cancelbuttontitle:@"ok" otherbuttontitles:nil, nil];     [alert show];     return; } nsstring *data = [nsstring stringwithformat:@"username=%@&password=%@",usernametextfield.text, passwordtextfield.text]; nsdata *postdata = [data datausingencoding:nsasciistringencoding allowlossyconversion:yes]; nsstring *postlength = [nsstring stringwithformat:@"%d", [postdata length]];  // preaparing url request send data. nsmutableurlrequest *request = [[nsmutableurlrequest alloc] init] ; nsurlconnection *theconnection=[[nsurlconnection alloc] initwithrequest:request delegate:self]; nsstring *url = [nsstring stringwithformat:@"http://www.ddproam.co.za/central/account/logonios?"]; [request seturl:[nsurl urlwithstring:url]]; [request sethttpmethod:@"post"]; [request setvalue:postlength forhttpheaderfield:@"content-length"]; [request sethttpbody:postdata]; [request settimeoutinterval:7.0];  nsurlresponse *response; nserror *error;  nsdata *urldata = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&error]; nsstring *str=[[nsstring alloc]initwithdata:urldata encoding:nsutf8stringencoding];  nslog(@"login response:%@",str);  nshttpcookie *cookie; nslog(@"name: '%@'\n",   [cookie name]);  (nshttpcookie *cookie in [[nshttpcookiestorage sharedhttpcookiestorage] cookies]) {     nslog(@"name: '%@'\n",   [cookie name]);     nslog(@"value: '%@'\n",  [cookie value]);     nslog(@"domain: '%@'\n", [cookie domain]);     nslog(@"path: '%@'\n",   [cookie path]); }   //parse out json data nsdictionary* json = [nsjsonserialization jsonobjectwithdata:urldata //1                                                      options:kniloptions                                                        error:&error];  nsarray* definejsondata = [json objectforkey:@"value"]; //2  nslog(@"value: %@", definejsondata); //3  if ([definejsondata isequal:0]) {     hud.customview = [[uiimageview alloc] initwithimage:[uiimage imagenamed:@"37x-checkmark.png"]];     hud.mode = mbprogresshudmodecustomview;     [hud hide:yes afterdelay:0];     uialertview *alert = [[uialertview alloc] initwithtitle:@"wrong credentials" message:@"please try login again" delegate:self cancelbuttontitle:@"ok" otherbuttontitles:nil, nil];     [alert show]; }  else {      hud.customview = [[uiimageview alloc] initwithimage:[uiimage imagenamed:@"37x-checkmark.png"]];     hud.mode = mbprogresshudmodecustomview;     [hud hide:yes afterdelay:0];     [self performseguewithidentifier: @"introscreenview" sender:self]; }   if (theconnection) {   } } 

if response server {"value":1}, correctly parse json dictionary using nsjsonserialization.

however under value key, there instance of nsnumber, not nsarray.

your code retrieve value , check should this:

nsnumber *definejsondata = [json objectforkey:@"value"];  nslog(@"value: %@", definejsondata);  if ([definejsondata integervalue] == 0) {     nslog(@"wrong credentials"); } else {     nslog(@"welcome :-)"); } 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -