Last active
April 5, 2022 02:19
-
-
Save mgersty/b565ba4c9e9422637f15f52a5317f07e to your computer and use it in GitHub Desktop.
Swift code demonstrating the capture of a 302 redirect via delegates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import PlaygroundSupport | |
PlaygroundPage.current.needsIndefiniteExecution = true | |
class DelegateToHandle302:NSObject, URLSessionTaskDelegate { | |
func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) { | |
print(response.description) | |
PlaygroundPage.current.finishExecution() | |
} | |
} | |
//set configurartions | |
let urlString = "[insert URL String]" | |
let config = URLSessionConfiguration.default | |
let url = URL(string: urlString) | |
//set delegate value equal to SessionDelegate to handle 302 redirect | |
let delegate = DelegateToHandle302() | |
//establish url session | |
let session = URLSession(configuration: config, delegate: delegate, delegateQueue: nil) | |
//set task with url | |
let dataTask = session.dataTask(with: url!) | |
//init call | |
dataTask.resume() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you !