ios - Return value in NSURLSession Error -
i below error while trying return responsestring
incompatible block pointer types sending 'nsstring *(^)(nsdata *__strong, nsurlresponse *__strong, nserror *__strong)' parameter of type 'void (^)(nsdata *__strong, nsurlresponse *__strong, nserror *__strong)'
where call suppose aviewcontroller class
nsstring *responsestring=[self callapi];
and below code in bviewmodel class:
-(nsstring* )callapi { nsurlsessionconfiguration *configuration = [nsurlsessionconfiguration defaultsessionconfiguration]; nsurlsession *session = [nsurlsession sessionwithconfiguration:configuration delegate:self delegatequeue:nil]; nsurl *url = [nsurl urlwithstring:@"http://appersgroup.com/talkawalk/login.php?email=twalknet@gmail.com&password=123456&latitude=52.486245&longitude=13.327496&device_token=show"]; nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:60.0]; [request addvalue:@"application/json" forhttpheaderfield:@"content-type"]; [request addvalue:@"application/json" forhttpheaderfield:@"accept"]; [request sethttpmethod:@"post"]; /* nsdictionary *mapdata = [[nsdictionary alloc] initwithobjectsandkeys: @"test ios", @"name", @"ios type", @"typemap", nil]; nsdata *postdata = [nsjsonserialization datawithjsonobject:mapdata options:0 error:&error]; [request sethttpbody:postdata]; */ nsurlsessiondatatask *postdatatask = [session datataskwithrequest:request completionhandler:^(nsdata *data, nsurlresponse *response, nserror *error) { nsstring* responsestring = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding]; if([responsestring rangeofstring:@"nil"].location != nsnotfound) { nsstring * newresponse = [[nsstring alloc] initwithdata:data encoding:nsasciistringencoding]; responsestring = newresponse; } nslog(@"%@",responsestring); nslog(@"response %@",response); nslog(@"error %@",error); return responsestring;//adding line give me error ? how return value }]; [postdatatask resume]; }
you can use method block :
-(void)callapiwithcompletionhandler : (void (^) (nsstring * strresponse)) completionhandler { nsurlsessionconfiguration *configuration = [nsurlsessionconfiguration defaultsessionconfiguration]; nsurlsession *session = [nsurlsession sessionwithconfiguration:configuration delegate:self delegatequeue:nil]; nsurl *url = [nsurl urlwithstring:@"http://appersgroup.com/talkawalk/login.php?email=twalknet@gmail.com&password=123456&latitude=52.486245&longitude=13.327496&device_token=show"]; nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:60.0]; [request addvalue:@"application/json" forhttpheaderfield:@"content-type"]; [request addvalue:@"application/json" forhttpheaderfield:@"accept"]; [request sethttpmethod:@"post"]; /* nsdictionary *mapdata = [[nsdictionary alloc] initwithobjectsandkeys: @"test ios", @"name", @"ios type", @"typemap", nil]; nsdata *postdata = [nsjsonserialization datawithjsonobject:mapdata options:0 error:&error]; [request sethttpbody:postdata]; */ nsurlsessiondatatask *postdatatask = [session datataskwithrequest:request completionhandler:^(nsdata *data, nsurlresponse *response, nserror *error) { nsstring* responsestring = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding]; if([responsestring rangeofstring:@"nil"].location != nsnotfound) { nsstring * newresponse = [[nsstring alloc] initwithdata:data encoding:nsasciistringencoding]; responsestring = newresponse; } nslog(@"%@",responsestring); nslog(@"response %@",response); nslog(@"error %@",error); completionhandler(responsestring); }]; [postdatatask resume]; }
call method , return response below :
[self callapiwithcompletionhandler:^(nsstring *strresponse) { nsstring *responsestring = strresponse; }];
suppose method implemented in viewcontrollera
, , want call viewcontrollerb
.
then import viewcontrollera
in viewcontrollerb
, make instance of viewcontrollera
in viewcontrollerb
.
viewcontrollera *vca = [[viewcontrollera alloc] init]; [vca callapiwithcompletionhandler:^(nsstring *strresponse) { nsstring *responsestring = strresponse; }];
Comments
Post a Comment