ios - Fetch data using Post method in Objective-c -
i fetching data using post method. , have retrieved data.it's taking long display in ui can print on console, code is
nsmutableurlrequest *request = [[nsmutableurlrequest alloc] init]; [request seturl:[nsurl urlwithstring:@"http://www.xxxyyy.com/v1/api/client/authorize"]]; [request sethttpmethod:@"post"]; [request setvalue:@"abcd" forhttpheaderfield:@"authkey"]; [request setvalue:@"application/x-www-form-urlencoded" forhttpheaderfield:@"content-type"]; nsurlsession *session = [nsurlsession sessionwithconfiguration:[nsurlsessionconfiguration defaultsessionconfiguration]]; [[session datataskwithrequest:request completionhandler:^(nsdata *data, nsurlresponse *response, nserror *error) { nsstring *requestreply = [[nsstring alloc] initwithdata:data encoding:nsasciistringencoding]; [request setvalue:@"application/json;charset=utf-8" forhttpheaderfield:@"authkey"]; nsdata* data1 = [requestreply datausingencoding:nsutf8stringencoding]; jsonreturnarray = [nsjsonserialization jsonobjectwithdata:data1 options:nsjsonreadingallowfragments error:&error]; nsarray *array = [jsonreturnarray copy]; [self rec:array]; nsstring *phonenumber=[nsstring stringwithformat:@"%@",[jsonreturnarray valueforkey:@"phone"]]; lblphonenumber.text = phonenumber; nsstring *address=[nsstring stringwithformat:@"%@ %@ %@,ca %@",[jsonreturnarray valueforkey:@"street1"],[jsonreturnarray valueforkey:@"street2"],[jsonreturnarray valueforkey:@"city"],[jsonreturnarray valueforkey:@"postalcode"]]; lbladdress.text=address;//takes long time display nslog(@"%@",address);//immeaditely print strlatitude=[jsonreturnarray valueforkey:@"latitude"]; strlongitude=[jsonreturnarray valueforkey:@"longitude"]; [self map:(mkmapview *)maplocation didupdateuserlocation:(mkuserlocation *)nil];//method call }] resume];
this take time print data, if use nsurlconnection class may you.this class method may helpful.
+ (void)postrequestdata:(nsdictionary *)postvars action:(apimode)action withcompletionhandlar:(void (^) (id result, bool status))completionblock { nsurl *url = [nsurl urlwithstring:api_url([self getapinamefortype:action])]; nslog(@"request url %@",[nsstring stringwithformat:@"%@",url]); nsstring *contenttype = @"application/json"; nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url]; [request sethttpmethod:@"post"]; [request addvalue:contenttype forhttpheaderfield: @"content-type"]; nserror *err = nil; nsmutabledictionary *params=[[nsmutabledictionary alloc] initwithdictionary:postvars]; // [params setobject:[self getapinamefortype:action] forkey:@"mode"]; nslog(@"paramater %@",params); nsdata *body = [nsjsonserialization datawithjsonobject:params options:nsjsonwritingprettyprinted error:&err]; [request sethttpbody:body]; [request addvalue:[nsstring stringwithformat:@"%lu", (unsigned long)body.length] forhttpheaderfield: @"content-length"]; [nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue mainqueue] completionhandler:^(nsurlresponse *response, nsdata *data, nserror *connectionerror) { if(!connectionerror) { nserror *error = nil; nsdictionary *dictresponse = [nsdictionary dictionarywithdictionary:[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments error:&error]]; dispatch_async(dispatch_get_main_queue(), ^{ completionblock(dictresponse,(error == nil)); }); nslog(@"%@",dictresponse); } else { dispatch_async(dispatch_get_main_queue(), ^{ completionblock(connectionerror.localizeddescription,no); }); } }]; }
use method instead of it.it executed fast because nsurlconnection class execute in background.
Comments
Post a Comment