28

I'm trying to configure Alamofire to follow redirects (or not) on a per-request basis.

Alamofire has a private internal class SessionDelegate which serves as the NSURLSessionTaskDelegate for the current URL session. SessionDelegate does implement the relevant delegate method, URLSession(session:, task:, willPerformHTTPRedirection response:, request:, completionHandler:) which is exactly what I want.

Even better, the delegate's implementation consults a custom variable closure named taskWillPerformHTTPRedirection to determine how to handle the redirect - again, exactly what I want!

And as far as I can tell, that closure is always nil by default -- it is not assigned to internally by Alamofire -- which suggests that it is intended to let the user assign a closure to it.

The problem: I cannot access this private SessionDelegate class to assign a closure to its taskWillPerformHTTPRedirection variable. It is a private class and it is not visible to my Swift files. What is the proper means of configuring an Alamofire request to (not) follow redirects?

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
Jonathan Hersh
  • 1,073
  • 1
  • 8
  • 15
  • Alamofire as AFnetworking respects HTTP standards. so I asume it follows up redirects automatically. I know because I've changed domains before and the app would follow up the redirect with no problems. – Eddwin Paz Feb 15 '15 at 18:00
  • AFNetworking can be configured to not follow redirects. Alamofire certainly has the foundation to do the same. – Jonathan Hersh Feb 15 '15 at 18:15
  • As Alamofire is not that much code and is quite young library, I would just fork the library and implement the functionality myself using taskWillPerformHTTPRedirection as you correctly spotted. Then just submit a PR. It seems that @mattt put those hooks there in place from the beginning even if they are not yet used by Alamofire public API. – Teemu Kurppa Feb 17 '15 at 20:58
  • 1
    @TeemuKurppa: You are correct, of course, [so I did](https://github.com/Alamofire/Alamofire/pull/350). :) – Jonathan Hersh Feb 17 '15 at 22:16
  • This has been bothering me a long time. I didn't notice the selector switch at the bottom until I had another look thanks to this question. I just set the closure manually in Alamofire which doesn't look as nice as your solution but if anyone else sees this and needs a quick solution until this gets merged it should work. Thank you! – Tony Feb 24 '15 at 20:38
  • 1
    I opened a bounty to get this issue more attention. See also Mr. Hersh's initial implementation at https://github.com/Alamofire/Alamofire/pull/350. – Aaron Brager Apr 05 '15 at 18:37

3 Answers3

10

Flexible redirect handling is now in Alamofire thanks to another pull request and is available with Alamofire 1.2.0.

Jonathan Hersh
  • 1,073
  • 1
  • 8
  • 15
  • I create custom class of `SessionDelegate`. `Alamofire.SessionDelegate` property is not settable. How to stop re-direction in Alamofire. – pkc456 Jun 16 '17 at 09:55
7

You can use it like this

let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
let sessionDelegate = Manager.SessionDelegate()
sessionDelegate.taskWillPerformHTTPRedirectionWithCompletion = {
   (session: NSURLSession, task: NSURLSessionTask, response: NSHTTPURLResponse,
    newRequest: NSURLRequest, completionHandler: NSURLRequest? -> Void) in

    // do something
}

let manager = Manager(configuration: configuration, delegate: sessionDelegate)

Alamofire Manager keeps the delegate as strong so you can be sure

public let delegate: SessionDelegate

but remember willPerformHTTPRedirection

This method is called only for tasks in default and ephemeral sessions. Tasks in background sessions automatically follow redirects.

also good to read about fundamentals Handling Redirects and Other Request Changes

onmyway133
  • 45,645
  • 31
  • 257
  • 263
0

I think that issue has been discussed long time ago , check this issue

matt's answer was clear though :

I presume various closure-typed properties in SessionDelegate, TaskDelegate, DataTaskDelegate and DownloadTaskDelegate are intended to be used by clients to extend/override particular delegates behavior.


Actually, that's incorrect. These are implemented internally for the sake of completeness. Any functionality intended for the end user would be exposed on Manager or Request.

Anyway I see that the bounty is offered by Aaron , while Jonathan forked his own version , so why dont you use that fork ?

ProllyGeek
  • 15,517
  • 9
  • 53
  • 72