2016-09-19 59 views
0

我知道如何捕获重定向url的最终url,但我不知道如何将最终的url返回给委托人。如何在swift中将最终的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 
    } 

我们获得最终的URL后,我想最终的URL传递给另一个委托功能

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

所以你想要在completionHandler中返回的网址? –

回答

2

如果我正确理解我们的问题,你正在寻找一种方式根据此委托函数传回的请求设置任务。

我在过去处理过的方法是用newRequest对象启动一个新任务。就我而言,这是一个下载任务的话,在willPerformHTTPRedirection函数体:那么

let newDownloadTask = session.downloadTaskWithRequest(request) 
newDownloadTask.resume() 

这个新的任务将启动,并开始委托回调为之前。

UPDATE:

可能做到这一点的最好办法是创建活动的任务和相关网址的字典。我把在操场上一起下 - 你可以根据需要添加相关网址:

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

所以回调可以在带参数的方法中调用 –

相关问题