NSURLSessionDelegate with Alamofire

In this lesson

Accessing NSURLSessionDelegate or NSURLSessionTaskDelegate methods with Alamofire is a little unconventional so let’s get that out of the way now. Once you know one, they are all easy enough to implement. Alamofire actually relays the delegate events by way of some of its own custom closures that you can override to include the same functionality of the standard delegate syntax without ever touching or accessing the actual delegates and protocols.

Kyle Roberts
Swift Guru at Large

Kyle's Series


Tap on time to skip ahead



Hello world, Kyle here. Let’s talk about accessing the URLSessionDelegate when using Alamofire. So there’s kind of a weird non-standard syntax to get these delegate callbacks when accessing this delegate through Alamofire but it is pretty neat and it’s not too difficult. 


Getting a Reference to NSURLSessionDelegate

So the first thing we need to do is actually get a reference to the delegate, which we can access easily by way of our manager and then use the delegate property on there. If you’re not using a custom Alamofire manager, you can actually use the default Alamofire manager using Alamofire.Manager.sharedInstance(). And then once you have a reference to that delegate, you actually set closure properties on the delegate by the name of the different NSURLSessionDelegate methods and an example of that is all of these. 


NSURLSessionDelegate Methods

And most of these, I’m not really addressing anything, but I have typed them all out and I’m logging any action there to the console and returning what is necessary just to demonstrate. So any requests run with your manager that may run into these NSURLSessionDelegate methods will receive their callback privately within Alamofire but then Alamofire will relay that message by way of these closures here and when we set the closure, we do have access to the different parameters that are normally in those delegate methods. And I’ll show an example of that in just a second, all we do is set the closure by the same name of the standard delegate methods. And set that to a new closure, make sure to name any of those variables or parameters here, and then any of the content of the delegate method that you would normally have. And some of those, to point out, do require return statements and in this case there’s three of them and one that does not, so in each of them I’m just printing if the delegate callback was hit. 



And then returning only what’s necessary to continue standard use of the app. Not really doing anything custom or taking any additional action here, that’s not the purpose of this video, it’s just to demonstrate how to access this delegate. So to compare to this delegate, let’s open it up. So I have scrolled way down after typing that, we’re actually in the NSURLSession header Swift file whatever this is in Foundation and we can see the NSURLSessionDataDelegate and the NSURLSessionTaskDelegate. We do have access to all of these delegate methods here and we can see an example of some of the ones that we’re using. For instance, here’s this one, URLSession:task:willPerformHTTPRedirection(withResponse):(and)completion:(handler). So that’s kind of a mouthful but this example here taskWillPerformHTTPRedirection and then we’re naming those four parameters here so that we have access to them and that’s how you access to a different URLSessionDelegates or TaskDelegates from Alamofire. 

Additional Info

Register to get access to additional resources and info.