-
Notifications
You must be signed in to change notification settings - Fork 494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
使用NSProxy的时候遇到问题了 #11
Comments
@hcl416029105 我先看看,晚上给你答复。 |
如果不用AFHTTPSessionManager,直接用原生的NSURLSession,没有问题:
这种可以正常调用。 好的,非常感谢你的解答 |
@hcl416029105 多谢提供的信息,我这边看了下,用 AFNetworking 出问题的原因是在 CFNetwork
但是因为你的
将属性改为 strong,发现问题已经没有了。
当然你可能会问为什么直接使用
事实上使用 具体你可以在自己项目中调试来验证,建议在 |
@hcl416029105 另外关于网络监控这块可以看下我的另外一篇文章揭秘 APM iOS SDK 的核心技术,文章对听云的网络监控实现进行了一些探索,希望能对你有所帮助。 |
网络监控搞复杂了,不用各种swizze,各种hook,直接取getifaddrs获取的数据 |
@hcl416029105 |
我在使用NSProxy替换NSURLSession原来的的delegate的时候,再使用AFNetwork的AFHTTPSessionManager的GET方法的时候崩溃了,不知道是什么原因,具体代码如下:
static void swizzleClassMethod(Class theClass, SEL originalSelector, SEL swizzledSelector)
{
Method origMethod = class_getClassMethod(theClass, originalSelector);
Method newMethod = class_getClassMethod(theClass, swizzledSelector);
}
@interface NSURLSession()
@Property(nonatomic, strong) NSURLDelegateProxy *delegateProxy;
@EnD;
@implementation NSURLSession (DelegateMonitor)
(void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
swizzleClassMethod([self class],
@selector(sessionWithConfiguration:delegate:delegateQueue:),
@selector(hcz_sessionWithConfiguration:delegate:delegateQueue:));
});
}
(NSURLSession *)hcz_sessionWithConfiguration:(NSURLSessionConfiguration *)configuration delegate:(nullable id )delegate delegateQueue:(nullable NSOperationQueue *)queue
{
if (delegate) {
NSURLDelegateProxy *proxy = [[NSURLDelegateProxy alloc] initWithTarget:delegate];
NSURLSession *session = [NSURLSession hcz_sessionWithConfiguration:configuration delegate:proxy delegateQueue:queue];
}
return [self hcz_sessionWithConfiguration:configuration delegate:delegate delegateQueue:queue];
}
@EnD
@interface NSURLDelegateProxy()
@Property(nonatomic, weak) id proxyTarget;
@EnD
@implementation NSURLDelegateProxy
(instancetype)initWithTarget:(id)target
{
self.proxyTarget = target;
return self;
}
(void)forwardInvocation:(NSInvocation *)invocation
{
SEL sel = [invocation selector];
if ( [self.proxyTarget respondsToSelector:sel]) {
[invocation invokeWithTarget:self.proxyTarget];
}
}
(nullable NSMethodSignature *)methodSignatureForSelector:(SEL)sel
{
NSMethodSignature *methodSignature = nil;
if ( [self.proxyTarget respondsToSelector:sel]) {
methodSignature = [self.proxyTarget methodSignatureForSelector:sel];
}
return methodSignature;
}
都是很简单的调用,AFHTTPSessionManager的调用方法如下:
错误提示:
2017-12-08 18:08:44.923871+0800 AFNetworkingDemo[82876:14788953] *** NSForwarding: warning: object 0x608000036560 of class '__NSMessageBuilder' does not implement doesNotRecognizeSelector: -- abort
The text was updated successfully, but these errors were encountered: