How to configure a proxy server in Swift?

I just started using iOS technology and developed an iPhone app using Swift.
I am trying to request a server using the following code:

var url = NSURL(string: "http://someurl:8080/?type=Refresh") var request = NSURLRequest(URL: url!) var connection = NSURLConnection(request: request, delegate: self, startImmediately: true) 

But I need to configure a proxy server on my iPhone device if I need to request this server. So now I want to configure the HTTP proxy (server and port) in the Swift code itself.

I reviewed the Apple CFProxySupport Handbook , but did not understand how I can use it. I have written the following code so far:

 var proxy_server: CFString = "proxy" // proxy server var proxy_port: CFNumber = 8080 // port var keys: [CFStringRef] = [kCFProxyTypeKey, kCFProxyHostNameKey, kCFProxyPortNumberKey] var values: [CFTypeRef] = [kCFProxyTypeHTTP, proxy_server, proxy_port] var proxy_dict: CFDictionary = CFDictionaryCreate( kCFAllocatorDefault, UnsafeMutablePointer<UnsafePointer<Void>>(keys), UnsafeMutablePointer<UnsafePointer<Void>>(values), 3, nil, nil) var proxies: Unmanaged<CFArray> = CFNetworkCopyProxiesForURL(NSURL(string: "http://someurl:8080"), proxy_dict) 

Can anyone tell me how to use proxies to install a proxy server?
Thanks!

+5
source share
1 answer

The CFProxySupport allows your application to receive proxies configured on the device. Some of these proxy configurations may actually be the URL of the automatic proxy / script - therefore CFNetworkCopyProxiesForURL can evaluate these scripts to determine the proxies that apply to the specified URL.

CFProxySupport does not allow you to configure the proxies that the device will use. In addition, NSURLConnection does not perform device proxy settings. You can use the code you need to get the configured proxy server and then use it with a library such as NSURLSession or AFNetworking that supports the use of a proxy server. In NSURLSession, you specify the proxy configuration in the connectionProxy dictionary of the NSURLSessionConfiguration object

+1
source

Source: https://habr.com/ru/post/1209364/


All Articles