taskWillPerformHTTPRedirection从未调用过

时间:2017-01-23 18:16:09

标签: ios swift3 alamofire

我正在尝试从我的帖子请求中获取重定向链接,但在升级到alamofire 4.3.0之后,我的delegate.taskWillPerformHTTPRedirection从未调用过。 我做错了什么?

我这样称呼:

let sessionManager = Alamofire.SessionManager(configuration: URLSessionConfiguration.default)
let delegate: Alamofire.SessionDelegate = sessionManager.delegate

delegate.taskWillPerformHTTPRedirection = { session, task, response, request in
    let headers = response.allHeaderFields
    MYUrlConstant.redirectLocation = headers["Location"]! as! String
    return URLRequest(url: URL(string: MYUrlConstant.redirectLocation)!)
}

Alamofire.request("http://test.com", method: .post, parameters: nil).responseJSON {}

1 个答案:

答案 0 :(得分:1)

我的问题:没有使用声明的管理员拨打我期望重定向的电话。 例如:

 let sessionManager = Alamofire.SessionManager(configuration: URLSessionConfiguration.default)

    let configuration = URLSessionConfiguration.default
    manager = Alamofire.SessionManager(configuration: configuration)
    let delegate: Alamofire.SessionDelegate = SessionDelegate.init()

    manager!.delegate.taskWillPerformHTTPRedirection = { session, task, response, request in
        let headers = response.allHeaderFields
        let redirectLocation = headers["Location"]! as! String
        return URLRequest(url: URL(string: redirectLocation)!)
    }

//make your call with your manager delcared
manager!.request("url", method: .post, parameters: parameters).responseJSON {    
}