Search code examples
swiftafnetworking

AFNetworking Migration Swift


I'm trying to make an HTTP POST request to my own server using AFNetworking:

let manager = AFHTTPSessionManager()
    manager.post(
        URL,
        parameters: params,
        success:
        {
            (operation, responseObject) -> Void in

            if let response = responseObject as? [String: String] {
                let alert = UIAlertController(title: response["status"],
                                              message: response["message"],
                                              preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "OK", style: .default) {_ in })
                self.present(alert, animated: true)
            }
        },
        failure:
        {
            (operation, error) -> Void in
            self.handleError(error as NSError)
        })

However, I'm getting the following error:

post(_:parameters:success:failure:) is deprecated

I searched the AFNetworking migration guide but that seems to only provide instructions in Objective-C, not Swift. I cannot find the new post method in Swift. Any ideas? Thanks!


Solution

  • If your project is written in Swift, why don't you use the Alamofire?

    As @luke-barry explained:

    AFNetworking and Alamofire are by the same people (the Alamofire Software Foundation), Alamofire is their Swift version whereas AFNetworking is the Objective-C version.

    Feature wise they are the same.

    See Swift Alamofire VS AFNetworking for more information.

    Hope this helps you!