Alamofire: How to use a Proxy Server with Alamofire

Created on 6 Mar 2017  路  2Comments  路  Source: Alamofire/Alamofire

I'm trying to use my Proxy for an API request that needs a specified IP.
To debug my issue, I'm requesting the IP from a webservice.

This is my current code:

    import UIKit
    import Alamofire

    class ViewController: UIViewController {

        var requestManager = Alamofire.SessionManager.default

        override func viewDidLoad() {
            super.viewDidLoad()
        }

        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(true)

            var proxyConfiguration = [NSObject: AnyObject]()
            proxyConfiguration[kCFNetworkProxiesHTTPProxy] = "http://[email protected]" as AnyObject?
            proxyConfiguration[kCFNetworkProxiesHTTPPort] = "9293" as AnyObject?
            proxyConfiguration[kCFNetworkProxiesHTTPEnable] = 1 as AnyObject?

            let cfg = Alamofire.SessionManager.default.session.configuration
            cfg.connectionProxyDictionary = proxyConfiguration

            let ip = URL(string: "https://api.ipify.org?format=json")

            requestManager = Alamofire.SessionManager(configuration: cfg)
            requestManager.request(ip!).response { response in
                print("Request: \(response.request)")
                print("Response: \(response.response)")
                print("Error: \(response.error)")

                if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
                    print("Data: \(utf8Text)")
                }
            }
        }
    }

The problem: the responsed IP is the same with or without the proxyConfiguration. Any help is very appreciated.

PS: physical device used.

support

All 2 comments

As you can read here, this should be on Stack Overflow.

@davidseek You need to setup your URLSessionConfiguration by creating a new instance. Modifying values on the current configuration doesn't have any effect, as per Apple's documentation. In the future, you should open questions like this on Stack Overflow and tag alamofire.

Cheers. 馃嵒


From our Contribution Guidelines

Asking Questions

We don't use GitHub as a support forum. For any usage questions that are not specific to the project itself, please ask on Stack Overflow instead. By doing so, you'll be more likely to quickly solve your problem, and you'll allow anyone else with the same question to find the answer. This also allows maintainers to focus on improving the project for others.

Was this page helpful?
0 / 5 - 0 ratings