2016-09-19 7 views
0

私は、リダイレクトされたURLの最終的なURLを取得する方法を知っていますが、最終的なURLを委譲者に返す方法はわかりません。私たちは、最終的なURLを取得した後、私は正しくご質問を理解していれば、あなたが方法を探している最後のURLを迅速に委任者に返す方法は?

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { 
     thenDoSomething() 
     } 
    } 
+0

あなたは完成したURLを返信しますか? –

答えて

2

別のデリゲート関数に、最終的な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() 

私はあなたがロジックに従うことができ、これはそれをクリア願っています!

+0

これは近いですが、私のアップデートを見ていただけますか?新しい追加されたデリゲート関数は最終的なURLをどのように受け取ることができますか? –

+0

私が探しているものとまったく同じです。ありがとう! –

-1

funcメソッドの右側に->を追加し、return request.urlを追加する必要があります。 以下のように: func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) -> NSURL { return request.url //send this final url back so that I can call it in another delegate }

+0

いいえ、URLRequestを取得するために、署名にコールバックがあります。 – Moritz

+0

コールバックはパラメータを持つメソッドで呼び出すことができます –

関連する問題