ios - Get NSURLConnection response (from a helper class) inside method of a different class -


i have class, "webapi", handles web api calls, class uses nsurlconnection through asynchronous delegate-based calls.

whenever object needs communicate web api use instance of webapi , call required method shown below in case of signing in make folowing call appdelegate:

- (void)applicationdidfinishlaunching:(nsnotification *)anotification {      webapi *webapi = [[webapi alloc] init];      [webapi performloginwithusername:@"test1@myserver.com" andpassword:@"password"]; } 

the problem once performloginwithusername:andpassword call made, code progresses on , any/all response received in delegate methods implemented in webapi.m.

this real issue because need able response codes , data received within class method call webapi, originated . able :

- (void)applicationdidfinishlaunching:(nsnotification *)anotification {      webapi *webapi = [[webapi alloc] init];      webapiresponse * webapirespnse = [webapi performloginwithusername:@"test1@myserver.com" andpassword:@"password"]; } 

where webapiresponse class custom class contain http status code , data received.

this achievable if change webapi.m use nsurlconnection sendsynchronousrequest, doesnt enable me receive http codes.

what best way fulfill requirement?

thank help.

you use blocks handle responses. example:

webapi.h - (void)performloginwithusername:(nsstring *)username                       andpassword:(nsstring *)password                     successblock:(void(^)(nsdata *response))successblock                     failureblock:(void(^)(nserror *error))failureblock;  webapi.m @interface webapi()     @property (nonatomic, copy) void(^authorizationsuccessblock)(nsdata *response);     @property (nonatomic, copy) void(^authorizationfailureblock)(nserror *error); @end  @implementation webapi - (void)performloginwithusername:(nsstring *)username                       andpassword:(nsstring *)password                     successblock:(void(^)(nsdata *response))successblock                     failureblock:(void(^)(nserror *error))failureblock {     self.authorizationsuccessblock = successblock;     self.authorizationfailureblock = failureblock;     // nsurlconnection call authorization here }  - (void)connectiondidfinishloading:(nsurlconnection *)connection {     if (self.authorizationsuccessblock != nil) {         self.authorizationsuccessblock(data);         self.authorizationsuccessblock = nil;     } }  - (void)connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error {     if (self.authorizationfailureblock != nil) {         self.authorizationfailureblock(error);         self.authorizationfailureblock = nil;     } }  appdelegate.m - (void)applicationdidfinishlaunching:(nsnotification *)anotification {    webapi *webapi = [[webapi alloc] init];    [webapi performloginwithusername:@"test1@myserver.com" andpassword:@"password" successblock:^(nsdata *response) {       // handle result here    } failureblock:^(nserror *error) {       // handle error here    }]; 

}


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -