简体   繁体   中英

Why can't I initialize or make a request with SessionManager in Alamofire 4.0? (UPDATED)

Migrating to Alamofire 4 and I am trying to initialize a session manager like so:

let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 20
configuration.httpAdditionalHeaders = ["MyCompany-User-Agent": Config.MyCompanyUserAgentDataString]
var alamoManager: SessionManager = Alamofire.SessionManager(configuration: configuration)

However I keep getting the error as

Cannot invoke SessionManager with an argument list of type(configuration: URLSessionConfiguration)

I am following the syntax at https://github.com/Alamofire/Alamofire#modifying-the-session-configuration , but mine has errors. What am I doing wrong here? Also I can't do a request from the SessionManager .

For example:

Alamofire.request(url, method: .get, parameters: parameters, encoding: JSONEncoding.default).responseObject { (response: DataResponse<MyCustomResponse>) in
            if let result = response.result.value {} }

Works with no issues, BUT (where alamoManager is a SessionManager):

alamoManager.request(url, method: .get, parameters: parameters, encoding: JSONEncoding.default).responseObject { (response: DataResponse<MyCustomResponse>) in
            if let result = response.result.value {} }

It doesn't work. I get the error as

Value of type SessionManager has no member request

Very confused as to why I am getting that error. The docs clearly show an example of a SessionManager object using a request method.

I have noticed though that there are no issues with this code if I create a new project. However in my existing project these errors display. I have checked the Podfile.lock and the version of Alamofire is indeed 4.0. I have build settings to target iOS9.0 for all targets and the project.

Could someone help me with this please? Tearing my hair out a bit!

I Suggest you initialize the extra headers with Alamofire's default Headers:

    // Adding Headers:
var defaultHeaders = Alamofire.SessionManager.defaultHTTPHeaders
defaultHeaders["MyCompany-User-Agent"] = Config.MyCompanyUserAgentDataString
    // adding Configuration:
let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = defaultHeaders 
configuration.timeoutIntervalForRequest = 20
var alamoManager: SessionManager = Alamofire.SessionManager(configuration: configuration)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM