title: How to capture redirect URL in Swift (after auth step of OAuth2)
body: <p>I am new to Swift. I need help in capturing the redirect URL after a user authorizes my application to use their account. The redirect URL contains the auth_code that I would exchange for a token. </p>
  
  <p>I am spinning in circles trying to use NSURLSessionDataDelegate: </p>
  
  <pre><code>class MySessionDelegate: NSObject, NSURLSessionDataDelegate {
      func URLSession(_: NSURLSession, task: NSURLSessionTask, willPerformHTTPRedirection: NSHTTPURLResponse, newRequest: NSURLRequest, completionHandler: (NSURLRequest!) -&gt; Void) {
          println(newRequest)
          println("hello")
      }
  }
  </code></pre>
  
  <p>but it is not being called in:</p>
  
  <pre><code>var authUrl = NSURL(string: "https://www.linkedin.com/uas/oauth2/authorization?response_type=\(responseType)" +
              "&amp;client_id=\(apiKey)" +
              "&amp;state=\(randomState)" +
              "&amp;redirect_uri=\(redirectUrl)")
  
  var session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration(), delegate: MySessionDelegate(), delegateQueue: nil)
  var request = NSMutableURLRequest(URL: authUrl!)
  
  myWebView.loadRequest(request)
  </code></pre>
  
  <p>I then added a NSURLProtocol to print the requests (I can see the redirect URL in the console), but this applies to all requests in the session which is not optimal:</p>
  
  <pre><code>var requestCount = 0
  
  class urlProtocol: NSURLProtocol {
  override class func canInitWithRequest(request: NSURLRequest) -&gt; Bool {
      println("Request #\(requestCount++): URL = \(request.URL.absoluteString)")
      return false
      }
  }
  </code></pre>
  
  <p>Thanks in advance.</p>