How to use a Proxy Server with Alamofire 4 and Swift 3

Please don’t mark as duplicate, I haven’t been able to solve my Issue with the existing threads.

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.

  • Customise the quicktype suggestions in iOS Keyboard
  • xcode file mark boxes in Project Navigator
  • How to write a data in plist?
  • NSTimer doesn't stop with invalidate
  • iOS 11 Navigation TitleView misplaced
  • How to convert an Int to a Character in Swift
  • 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://xxx@eu-west-static-01.quotaguard.com" 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.

    2 Solutions Collect From Internet About “How to use a Proxy Server with Alamofire 4 and Swift 3”

    I think the working (supposed to be deprecated) keys are:

    kCFStreamPropertyHTTPSProxyHost
    kCFStreamPropertyHTTPSProxyPort
    

    Could you try this 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] = "eu-west-static-01.quotaguard.com" as AnyObject?
             proxyConfiguration[kCFNetworkProxiesHTTPPort] = "9293" as AnyObject?
             proxyConfiguration[kCFNetworkProxiesHTTPEnable] = 1 as AnyObject?
             proxyConfiguration[kCFStreamPropertyHTTPSProxyHost as String] = "eu-west-static-01.quotaguard.com"
             proxyConfiguration[kCFStreamPropertyHTTPSProxyPort as String] = 9293
             proxyConfiguration[kCFProxyUsernameKey as String] = xxx
             //proxyConfiguration[kCFProxyPasswordKey as String] = "pwd if any"
            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)")
                }
            }
        }
    }
    

    Also please make sure your proxy server is configured to handle https requests.

    Note: It might give deprecated warning for those keys but keys are still working (see https://forums.developer.apple.com/thread/19356#131446)

    Note: I posted the same answer here. Posting the same here as it was applicable here as well. Alamofire is using same URLSessionConfiguration.

    Based off Manishg’s answer I use the following to avoid warnings

    let configuration = URLSessionConfiguration.default
    configuration.httpAdditionalHeaders = SessionManager.defaultHTTPHeaders
    
    var proxyConfiguration = [String: AnyObject]()
    proxyConfiguration.updateValue(1 as AnyObject, forKey: "HTTPEnable")
    proxyConfiguration.updateValue("eu-west-static-01.quotaguard.com" as AnyObject, forKey: "HTTPProxy")
    proxyConfiguration.updateValue(9293 as AnyObject, forKey: "HTTPPort")
    proxyConfiguration.updateValue(1 as AnyObject, forKey: "HTTPSEnable")
    proxyConfiguration.updateValue("eu-west-static-01.quotaguard.com" as AnyObject, forKey: "HTTPSProxy")
    proxyConfiguration.updateValue(9293 as AnyObject, forKey: "HTTPSPort")
    configuration.connectionProxyDictionary = proxyConfiguration
    
    sharedManager = Alamofire.SessionManager(configuration: configuration)
    

    The https constants have been deprecated and could be removed at anytime. By using the string values, the code might break but it won’t crash