我的 博客地址
最近研究了下 connectionProxyDictionary,做一個簡單的筆記。官方文檔是這么描述的。
This property controls which proxy tasks within sessions based on this configuration use when connecting to remote hosts.
The default value is NULL, which means that tasks use the default system settings.
這個屬性可以設置網(wǎng)絡代理,默認值是 NULL,使用系統(tǒng)的代理設置。
有一個比較巧妙的用法,可以通過設置為空字典可以禁止代理抓包(charles、fiddler等)。
上代碼。
NSString* proxyHost = @"192.168.12.23";//@"myProxyHost.com";
NSNumber* proxyPort = [NSNumber numberWithInt: 12345];
// 創(chuàng)建一個代理服務器,包括HTTP或HTTPS代理,當然還可以添加SOCKS,FTP,RTSP等
NSDictionary *proxyDict = @{
(NSString *)kCFNetworkProxiesHTTPEnable : [NSNumber numberWithInt:1],
(NSString *)kCFNetworkProxiesHTTPProxy: proxyHost,
(NSString *)kCFNetworkProxiesHTTPProxyPort: proxyPort,
(NSString *)kCFNetworkProxiesHTTPSEnable : [NSNumber numberWithInt:1],
(NSString *)kCFNetworkProxiesHTTPSProxy: proxyHost,
(NSString *)kCFNetworkProxiesHTTPSProxyPort: proxyPort,
};
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
// 設置代理
configuration.connectionProxyDictionary = proxyDict;
// 禁止代理
configuration.connectionProxyDictionary = @{};
參考資料
1、Apple 文檔
2、How to programmatically add a proxy to an NSURLSession