ios - Accessing delegate of NSURLSessionTasks wrapped in static method in separate class -
i wrote separate webservices class app - webservices.m.
in it, have several static methods such setuserinput(), getuserprofile(), registernewuser(), logoutofaccount(). in methods include various nsurlrequests , nsurlsession tasks hit server's endpoints.
i can call these static methods in various viewcontrollers - [webservices registernewuser]. issue is, want following:
- access response item task
- go new viewcontroller once task finished
i have been using completion blocks within each completionhandler: assume i'm trying do, need using delegates instead? if so,
- how access delegates if i'm using static methods in other classes?
- how can each delegate differentiate between different tasks calling them?
examples appreciated since i'm pretty new , couldn't find pertinent on stackoverflow.
thank you!
edit:
example of static method in webservices.m completion handler.
+(void)logoutaccount{ // 1 nsurl *url = [nsurl urlwithstring:@"https://mywebsite.com/logout/"]; nsurlsessionconfiguration *sessionconfig = [nsurlsessionconfiguration defaultsessionconfiguration]; sessionconfig.httpadditionalheaders = @{@"authorization": @"token 123456678809203490249019203"}; nsurlsession *session = [nsurlsession sessionwithconfiguration:sessionconfig]; // 2 nsmutableurlrequest *request = [[nsmutableurlrequest alloc] initwithurl:url]; request.httpmethod = @"post"; // 3 nsdictionary *dictionary = @{}; nserror *error = nil; nsdata *data = [nsjsonserialization datawithjsonobject:dictionary options:kniloptions error:&error]; if(!error){ nsurlsessionuploadtask *uploadtask = [session uploadtaskwithrequest:request fromdata:data completionhandler:^(nsdata *data,nsurlresponse *response,nserror *error) { // handle response here nshttpurlresponse *httpresponse = (nshttpurlresponse*)response; nslog(@"%i",httpresponse.statuscode); }]; // 5 [uploadtask resume]; } }
in viewcontroller.m viewdidload() call [webservices logoutaccount]. how access nsurlresponse of completionhandler? should using delegates right? why asking questions above :)
- completion blocks
as url session programming guide states
note: completion callbacks intended alternative using custom delegate. if create task using method takes completion callback, delegate methods response , data delivery not called.
so, ok go completion blocks in case, however,
issue #1: code not pass callbacks caller (e.g. viewcontroller).
in webservices's header:
typedef void (^webservicescompletionhandler)(id responseobject, nserror *error); + (void)logoutaccountwithcompletionhandler:(webservicescompletionhandler)completionblock;
this method, them used
issue #2: use nsurlsessionuploadtask
api (log out) should not upload data server. again, don't know server api, rather go nsurlsessiondatatask
api.
nsurlsessiondatatask* datatask = [session datataskwithrequest:request completionhandler:^(nsdata * _nullable data, nsurlresponse * _nullable response, nserror * _nullable error) { // process response, data , error } [datatask resume];
issue #3: code not call back
// if result api processed ui, pass them in main queue dispatch_async_main(^{ // make sure caller has passed completionblock if (completionblock) { completionblock(responseobject, error); } });
so, complete method this:
+ (void)logoutaccountwithcompletionhandler:(webservicescompletionhandler)completionblock { nsurlsessionconfiguration *sessionconfig = [nsurlsessionconfiguration defaultsessionconfiguration]; sessionconfig.httpadditionalheaders = @{@"authorization": @"token 123456678809203490249019203"}; nsurlsession *session = [nsurlsession sessionwithconfiguration:sessionconfig]; nsurl *url = [nsurl urlwithstring:@"https://mywebsite.com/logout/"]; nsmutableurlrequest *request = [[nsmutableurlrequest alloc] initwithurl:url]; request.httpmethod = @"post"; nsurlsessiondatatask* datatask = [session datataskwithrequest:request completionhandler:^(nsdata * _nullable data, nsurlresponse * _nullable response, nserror * _nullable error) { dispatch_async_main(^{ // make sure caller has passed completionblock if (completionblock) { completionblock(response, error); } }); }]; [datatask resume]; }
you can use viewcontroller
webservicescompletionhandler completionhandler; completionhandler = ^(id response, nserror* error) { if (error == nil) { // process response object } else { // process error } }; [webservices logoutaccountwithcompletionhandler: completionhandler];
- delegates
using delegates involves more things do, better find complete example, rather post here. now, think ok try out completion blocks solution. let me know.
p.s.: recommend go through several tutorial out there more familiar network communications (e.g. this one)
Comments
Post a Comment