ios – Alamofire 4->5 taskWillPerformHTTPRedirection Upgrade – Stack Overflow


Background: complete Swift/iOS development/Alamofire noob.

Trying to update an existing iOS project to latest minimum supported versions and I am running into issues where I am not sure how to update the following to Alamofire’s latest syntax:

static func check(urlString: String,
                  successBlock: SuccessBlock?,
                  errorHandlerBlock: ErrorHandlerBlock?) -> DataRequest? {
    
    var validURLString = ""
    
    if urlString.isContainsHTTP {
        validURLString = urlString
    } else {
        validURLString.append("http://")
        validURLString.append(urlString)
    }
    
    guard let url = URL(string: validURLString + APIMethod.check) else {
        errorHandlerBlock?(ErrorHandler("We could not complete the request at: \(validURLString)"))
        return nil
    }
    
    Alamofire.SessionManager.default.delegate.taskWillPerformHTTPRedirection = { session, task, response, request in
        return URLRequest(url: url)
    }
    
    return Alamofire.request(url,
                             method: .get,
                             parameters: nil,
                             headers: httpHeaders(.get)).responseJSON { (response) in
                                
                                handle(response,
                                       successBlock: { _ in
                                        successBlock?(validURLString)
                                },
                                       errorHandlerBlock: { _ in
                                        errorHandlerBlock?(ErrorHandler("URL not supported"))
                                })
    }
}

For context, previous version of Alamofire used was 4.7.3 and am now updating to 5.8.1.

For Alamofire.SessionManager.default.delegate.taskWillPerformHTTPRedirection, I am currently getting the following errors:

Cannot infer type of closure parameter 'request' without a type annotation
Cannot infer type of closure parameter 'response' without a type annotation
Cannot infer type of closure parameter 'session' without a type annotation
Cannot infer type of closure parameter 'task' without a type annotation
Module 'Alamofire' has no member named 'SessionManager'

And I assume I can update to Alamofire.Session.default.delegate to resolve the last error. However, taskWillPerformHTTPRedirection is no longer a valid member and I am not sure how to update that.

In addition, for return Alamofire.request I am getting the following errors:

Module 'Alamofire' has no member named 'request'
Cannot infer contextual base in reference to member 'get'
'nil' requires a contextual type

I believe all three errors can be resolved by updating Alamofire.request to AF.request, but want to confirm that functionality would remain the same.

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img