Reputation: 263
I am trying to get back the response body of a url, which redirects to another invalid url. I would like to stop the redirection as well, since it crashes my app.
I have tried using the following methods within my code, but they aren't picked up:
func URLSession(_ session: NSURLSession,
dataTask dataTask: NSURLSessionDataTask,
didReceiveResponse response: NSURLResponse,
completionHandler completionHandler: (NSURLSessionResponseDisposition) -> Void) {
print("Did you get here?")
}
func URLSession(_ session: NSURLSession,
task task: NSURLSessionTask,
didCompleteWithError error: NSError?) {
print("How about here?")
}
func URLSession(_ session: NSURLSession,
task task: NSURLSessionTask,
willPerformHTTPRedirection response: NSHTTPURLResponse,
newRequest request: NSURLRequest,
completionHandler completionHandler: (NSURLRequest?) -> Void) {
print("Maybe here?")
}
func URLSession(_ session: NSURLSession,
didBecomeInvalidWithError error: NSError?) {
print("Did you find an error?")
}
Any ideas on what I can do?
EDIT: So, this is the updates code that I have that gives me no errors using XCode 7:
class Example: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate {
override init() {
super.init()
let mySession = NSURLSession(configuration: NSURLSessionConfiguration.ephemeralSessionConfiguration(), delegate: self, delegateQueue: NSOperationQueue.mainQueue())
// Put your handler as the second parameter.
let data = try!mySession.dataTaskWithURL(NSURL(string: "http://www.google.com")!, completionHandler: myHandler)
data!.resume()
}
// Create your handler with the following signature, and read the response body.
func myHandler(data: NSData?, response: NSURLResponse?, error: NSError?) -> Void {
// In this case the “encoding” is NSASCIIStringEnconding. It depends on the website.
let responseBody = NSString(data: data!, encoding: NSASCIIStringEncoding)
print(responseBody)
}
// Handles redirection
private func URLSession(session: NSURLSession, task: NSURLSessionTask, willPerformHTTPRedirection response: NSHTTPURLResponse, newRequest request: NSURLRequest, completionHandler: (NSURLRequest!) -> Void) {
// Stops the redirection, and returns (internally) the response body.
completionHandler(nil)
}
}
Upvotes: 4
Views: 5829
Reputation: 89
To stop a redirect and get the response body you should call the completionHandler
with nil
as the parameter.
After you stop the redirect, URLSession will call the handler you specified, when you created a HTTP Get request with dataTaskWithURL:completionHandler:
(could also be dataTaskWithRequest:completionHandler:
). Then in the handler you get the responde body. Example:
import Foundation
class Example: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate {
override init() {
super.init()
let mySession = NSURLSession(configuration: NSURLSessionConfiguration.ephemeralSessionConfiguration(), delegate: self, delegateQueue: NSOperationQueue.mainQueue())
// Put your handler as the second parameter.
let data = mySession.dataTaskWithURL(NSURL(string: "http://www.google.com")!, completionHandler: myHandler)
data.resume()
}
// Create your handler with the following signature, and read the response body.
func myHandler(data: NSData!, response: NSURLResponse!, error: NSError!) -> Void {
// In this case the “encoding” is NSASCIIStringEnconding. It depends on the website.
let responseBody = NSString(data: data, encoding: NSASCIIStringEncoding)
println(responseBody)
}
// Handles redirection
func URLSession(session: NSURLSession, task: NSURLSessionTask, willPerformHTTPRedirection response: NSHTTPURLResponse, newRequest request: NSURLRequest, completionHandler: (NSURLRequest!) -> Void) {
// Stops the redirection, and returns (internally) the response body.
completionHandler(nil)
}
}
I know it works with the HTTP 302 code, but for others like 301 (Moved Permanently) or 308 I don't know if it works.
Worth adding (about URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:
):
This method is called only for tasks in default and ephemeral sessions. Tasks in background sessions automatically follow redirects.
Upvotes: 4