ios - Can I have a NSURLSession delegate method in a different ViewController in iOS7? -


i using ios7 , xcode 5.

i have instantiated nsurlsession object url in 1 viewcontroller , want write delegate in different viewcontroller. possible?

basically, want make call in 1 viewcontroller, , after making call transit other viewcontroller. once transition made, want receive response in second viewcontroller through nsurlsession delegate methods , display data. possible?

note: don't want keep user waiting in first viewcontroller until server comes response, because while server processing data, can keep user engaged in second viewcontroller other images/elements displayed.

your api.h

#import <foundation/foundation.h> typedef void(^successtypeblock)(nsdata*); typedef void(^errortypeblock)(nserror*); @interface testapi : nsobject<nsurlconnectiondelegate>  -(void)hitapiwithurl:(nsurl*)url successblock:(successtypeblock)success  failureblock:(errortypeblock)failure; @end 

your api.m

@interface  testapi() {  } @property(nonatomic,copy) errortypeblock    errorblock_; @property(nonatomic,copy) successtypeblock  successblock_; @end @implementation testapi   -(void)hitapiwithurl:(nsurl*)url successblock:(successtypeblock)success  failureblock:(errortypeblock)failure{     errorblock_=failure;     successblock_=success;     nsurlrequest   *request=[nsurlrequest requestwithurl:url];     [nsurlconnection connectionwithrequest:request delegate:self];  }  - (void)connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response {  } - (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data  {     successblock_(data); }  - (void)connectiondidfinishloading:(nsurlconnection *)connection {    }  - (void)connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error {     errorblock_(error); }    @end 

now

[<yourapiobj>   hitapiwithurl:<your url> successblock:^(nsdata* data){ //get success value here after server responce  }  failureblock:^(nserror* error){  //get error if }]; 

just gave basic skeleton improve , innovate per needs.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -