我有一个iOS应用程序。我想修改defaultSessionConfiguration
,以便每次在代码后面调用[NSURLSessionConfiguration defaultSessionConfiguration]
时,我都会得到修改后的defaultSessionConfiguration
我试过这样的东西,但它没有工作
NSURLSessionConfiguration *configuration = NSURLSessionConfiguration.defaultSessionConfiguration;
// Set the proxy configuration options in the connectionProxyDictionary property
NSDictionary *proxyDict = @{
@"HTTPSEnable": [NSNumber numberWithInt:1],
@"HTTPSProxy": @"localhost",
@"HTTPSPort": [NSNumber numberWithInt:1234]
};
configuration.connectionProxyDictionary = proxyDict;
字符串
这一个给了我一个空connectionProxyDictionary的配置
NSURLSessionConfiguration *configuration2 = [NSURLSessionConfiguration defaultSessionConfiguration];
型
在我的应用程序中有一个lib,它向某个主机发出请求。我知道这个lib使用NSURLSessionConfiguration defaultSessionConfiguration
。我想修改“全局”defaultSessionConfiguration
,以便lib开始通过我的代理发送请求。
1条答案
按热度按时间iswrvxsc1#
您真正能做的唯一一件事就是混合该方法(我不能100%确定您不会遇到可能会阻止这样做的类集群异常)。
在高级别上,你会这样做:
imp_implementationWithBlock
. IIRC创建一个方法实现(IMP
),其参数应该是:class_getInstanceMethod
查找方法。method_setImplementation
替换该方法的实现。IMP
)存储在静态全局变量中。@selector(defaultSessionConfiguration)
)确保所有这些都在dispatch_once块中完成,例如。
字符串
以确保您不能重复此操作。
但是,请注意,在生产应用中这样做是相当危险的。最好尽可能要求框架开发人员提供钩子,以便传入您自己创建的会话(或您自己创建的配置)。
文档链接: