
Hands-on with a Proxy IP in Swift
Engaged in the network request know, sometimes directly with their own IP request easy to eat the door. Let's say when crawling data, the website anti-climbing mechanism is triggered, a minute to block your IP. At this time, if you can match a proxy IP, it is really a life-saving trick.
Let's take URLSession as an example and talk about how to organize proxy settings in Swift. Focus onURLSessionConfigurationThis configuration object, which has a connectionProxyDictionary property, stuffs proxy information into it and that's it.
let config = URLSessionConfiguration.default
config.connectionProxyDictionary = [
kCFNetworkProxiesHTTPEnable: 1,
kCFNetworkProxiesHTTPEnable: 1, kCFNetworkProxiesHTTPPort: 8080,
kCFNetworkProxiesHTTPProxy: "1.2.3.4",
kCFProxyUsernameKey: "Account number", // Fill in if you need authentication.
kCFProxyPasswordKey: "Password" // Delete these lines if you don't need it.
]
let session = URLSession(configuration: config)
Be careful, don't mix up the keys when you use the dictionary here. If you are using a socks5 proxy, remember to replace the key that starts with HTTP with a key related to socks. The 1.2.3.4 and 8080 in the code should be replaced with the IP and port of the proxy you actually get.
Which is a good choice for proxy IP?
There are many proxy service providers on the market, but there are also many pits. Some of them claim to be cheap, but when you use them, they either drop the line or slow down to a turtle. Here are some recommendationsipipgoThe service of the home and the IP quality of their homes is really solid.
| Package Type | Applicable Scenarios | prices |
|---|---|---|
| Dynamic residential (standard) | Routine data collection | 7.67 Yuan/GB/month |
| Dynamic Residential (Business) | High-frequency operational requirements | 9.47 Yuan/GB/month |
| Static homes | Long-term fixed IP requirements | 35RMB/IP/month |
A special word about theirTK LineThe delay can be reduced to less than 200ms when doing overseas business. The extraction method is also simple, directly adjust the API can get the latest IP, also supports socks5 protocol.
A practical guide to avoiding the pit
A few common mistakes made by newbies:
- IPs expire without replacementDynamic IP validity is only a few minutes, remember to refresh in time!
- mistaken agreement: http proxy in socks5 configuration, definitely not connecting
- Forgot to authenticate: Proxies with account passwords don't come with authentication information in the code, wait for the error to be reported
Tip to test whether the proxy is effective: first send a request to httpbin.org to see if the returned origin field is the proxy IP address. If it is still the local IP address, it means that the configuration has not taken effect.
Frequently Asked Questions
Q: What should I do if the request times out after the proxy is set?
A: First check the IP and port are not correct, and then try to use the curl command to connect directly to the proxy server, to rule out network problems!
Q: How to operate if I need to switch IP frequently?
A: It is recommended to use ipipgo's API to obtain IP dynamically, and update the connectionProxyDictionary before each request.
Q: How to choose between static IP and dynamic IP?
A: Need to maintain the session for a long time choose static, short-term high-frequency requests with dynamic more cost-effective
As a final reminder, don't just look at price when choosing a proxy service. The likes of ipipgo can provide1-to-1 customized programsThe special business needs of the time can really save the life. Their client tools are also quite foolproof, too much trouble writing code, if the direct client to switch a key more convenient.

