[英]taskWillPerformHTTPRedirection never called in Alamofire 5
Updating Alamofire to 5.0.4.将 Alamofire 更新到 5.0.4。 As the title says
taskWillPerformHTTPRedirection
is never called.正如标题所说,从不调用
taskWillPerformHTTPRedirection
。
In Alamofire 4.x we could do something like:在 Alamofire 4.x 中,我们可以执行以下操作:
let sessionDelegate = request.session.delegate as! Alamofire.SessionDelegate
sessionDelegate.taskWillPerformHTTPRedirection = { session, task, response, request in
if let url = task.currentRequest?.url {
// look at redirected url & act accordingly
}
}
}
A request's session/delegate has been overhauled in Alamofire 5 and is no longer directly accessible from the request.请求的会话/委托已在 Alamofire 5 中进行了大修,不再可以从请求中直接访问。 More specifically,
taskWillPerformHTTPRedirection
is a closure callback on ClosureEventMonitor
.更具体地讲,
taskWillPerformHTTPRedirection
是一个封闭的回调ClosureEventMonitor
。 As a sanity check, I tested using some of the other closure callbacks.. and they worked.作为健全性检查,我使用其他一些闭包回调进行了测试……并且它们起作用了。
// ClosureEventMonitor
let monitor = ClosureEventMonitor()
monitor.requestDidCreateTask = { request, task in
// Event fires
}
let monitor2 = ClosureEventMonitor()
monitor2.taskWillPerformHTTPRedirection = { sess, task, resp, req in
// Event Never fires
}
monitor2.requestDidFinish = { request in
// Event Fires
}
// Set up Session
var session: Session? = Session(startRequestsImmediately: false, eventMonitors: [monitor, monitor2])
let url = URL(string: "https://google.com")!
let urlRequest = URLRequest(url: url)
let trequest = session?.request(urlRequest)
For reference this code is being fired from my AppDelegate
func application(_ application: UIApplication, continue userActivity: NSUserActivity
for handling deep/universal links.作为参考,此代码是从我的
AppDelegate
func application(_ application: UIApplication, continue userActivity: NSUserActivity
for func application(_ application: UIApplication, continue userActivity: NSUserActivity
deep/universal links 中func application(_ application: UIApplication, continue userActivity: NSUserActivity
的。
I'm not exactly sure what I'm missing here.我不确定我在这里错过了什么。 Any help is greatly appreciated.
任何帮助是极大的赞赏。 Thank you for your time.
感谢您的时间。
There are three things here:这里有三件事:
First, session?.request(urlRequest)
will never actually make a request, since you never call resume()
(or attach a response handler).首先,
session?.request(urlRequest)
永远不会真正发出请求,因为您永远不会调用resume()
(或附加响应处理程序)。
Second, using a one off Session
like that is not recommended.其次,不建议使用像这样的一次性
Session
。 As soon as the Session
goes out of scope all requests will be cancelled.一旦
Session
超出范围,所有请求都将被取消。
Third, EventMonitor
s cannot interact with the request pipeline, they're only observational.第三,
EventMonitor
不能与请求管道交互,它们只是观察性的。 Instead, use Alamofire 5's new RedirectHandler
protocol or Redirector
type to handle redirects.相反,使用 Alamofire 5 的新
RedirectHandler
协议或Redirector
类型来处理重定向。 There is more in our documentation . 我们的文档中有更多内容。 A simple implementation that customizes the action performed would be:
自定义执行的操作的简单实现是:
let redirector = Redirector(behavior: .modify { task, request, response in
// Customize behavior.
})
session?.request(urlRequest).redirect(using: redirector)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.