Alamofire 4->5 taskWillPerformHTTPRedirection Upgrade

92 Views Asked by At

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.

1

There are 1 best solutions below

0
Jon Shier On

This has all changed in Alamofire 5, you may want to check the documentation.

SessionManager is now just Session.

Alamofire.request is now AF.request. The Alamofire there previously was never actually doing anything. The AF is a real instance, renamed because Swift doesn't like types and modules with the same name.

Redirects are handled by types conforming to the RedirectHandler protocol. You can add one to a request with .redirect(using:). You can also set one globally when you create a Session. But what you're doing in the redirect delegate in your original code doesn't do anything, redirects are followed automatically. An equivalent would be .redirect(using: .follow). You can read more in our docs.

I also suggest you update all your response parsing to use Decodable types and use .responseDecodable to parse them.