別のデリゲート関数に、最終的なURLを渡し
func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) {
request.url //send this final url back so that I can call it in another delegate
}
たいれる
このデリゲート関数によって返された要求に基づいてタスクを設定します。
これまで私がこれを処理してきた方法は、newRequestオブジェクトで新しいタスクを開始することです。私の場合、それはwillPerformHTTPRedirection関数の本体には、そのダウンロードタスクました:
let newDownloadTask = session.downloadTaskWithRequest(request)
newDownloadTask.resume()
この新しいタスクは開始し、以前のように、デリゲートのコールバックを作り始めます。
UPDATE:
おそらくこれを行うための最善の方法は、アクティブなタスクや関連URLで辞書を作成することです。私は運動場で一緒に以下を入れて - あなたは、必要に応じて、関連するURLを追加することができます。
import UIKit
import PlaygroundSupport
let currentPage = PlaygroundPage.current
currentPage.needsIndefiniteExecution = true
class downloader : NSObject, URLSessionDelegate, URLSessionDataDelegate {
var session : URLSession!
var tasks : [URLSessionDataTask : String] = [URLSessionDataTask : String]()
func startDownloadWithDelegateHandler() {
self.session = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: OperationQueue.main)
let urlString : String = < PUT YOUR URL HERE >
guard let url = URL(string: urlString) else { return }
let request : URLRequest = URLRequest(url: url)
let dataTask : URLSessionDataTask = session.dataTask(with: request)
self.tasks[dataTask] = urlString
dataTask.resume()
}
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
print("Data received")
print(dataTask.description)
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
if error != nil {
print(error)
}
else
{
print("Finished")
if let urlString = self.tasks[task as! URLSessionDataTask] {
print(urlString)
}
}
}
func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) {
print("Getting redirected")
print(response)
let newDataTask = self.session.dataTask(with: request)
self.tasks[newDataTask] = String(describing: request.url)
print(newDataTask.taskDescription)
newDataTask.resume()
}
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
completionHandler(URLSession.AuthChallengeDisposition.performDefaultHandling, nil)
}
func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
completionHandler(URLSession.AuthChallengeDisposition.performDefaultHandling, nil)
}
func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
if let _ = error {
print(error!)
}
}
}
let myDownloader = downloader()
myDownloader.startDownloadWithDelegateHandler()
私はあなたがロジックに従うことができ、これはそれをクリア願っています!
あなたは完成したURLを返信しますか? –