I have a class:
class Test: UIViewController, NSURLSessionDelegate {
func URLSession(session: NSURLSession, task: NSURLSessionTask, willPerformHTTPRedirection response: NSHTTPURLResponse, newRequest request: NSURLRequest, completionHandler: (NSURLRequest!) -> Void) {
let responseHeaderFields = (response ).allHeaderFields as! [String : String]
var nR = NSMutableURLRequest(URL: request.URL!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringCacheData, timeoutInterval: NSTimeInterval(30))
nR.allHTTPHeaderFields = NSHTTPCookie.requestHeaderFieldsWithCookies(NSHTTPCookie.cookiesWithResponseHeaderFields(responseHeaderFields, forURL: response.URL!))
for header in responseHeaderFields {
nR.setValue(
header.1,
forHTTPHeaderField: header.0
)
}
completionHandler(nR)
}
func myMethod() {
//some code...
let config = NSURLSessionConfiguration.ephemeralSessionConfiguration()
let session = NSURLSession(configuration: config, delegate: self, delegateQueue: nil)
session.dataTaskWithRequest(urlRequest, completionHandler: {
(data:NSData?, response:NSURLResponse?, error:NSError?) in
guard let responseData = data else {
return
}
guard error == nil else {
return
}
//....
}
}
}
When i call myMethod i do post request to add item to cart. This post request create cookie for cart and redirect me to another url. But my attempts to catch redirect and then set cookies and another headers data to next url is not working 😟 Any idea why ?