@@ -54,19 +54,21 @@ - (NSURLSessionDataTask *)greyswizzled_dataTaskWithRequest:(NSURLRequest *)reque
54
54
// Swizzle the session delegate class if not yet done.
55
55
id delegate = self.delegate ;
56
56
SEL originalSel = @selector (URLSession:task:didCompleteWithError: );
57
- Class delegateClass =
58
- [[delegate forwardingTargetForSelector: originalSel] class ] ?: [delegate class ];
59
-
57
+ // This is to solve issues where there is a proxy delegate instead of the original instance (e.g. TrustKit, New Relic).
58
+ // It responds YES to `respondsToSelector:` but `class_getInstanceMethod` returns nil.
59
+ // We attempt to follow the forwarding targets for the selector, if any exists.
60
+ id nextForwardingDelegate = nil ;
61
+ while ((nextForwardingDelegate = [delegate forwardingTargetForSelector: originalSel]) != nil )
62
+ {
63
+ delegate = nextForwardingDelegate;
64
+ }
65
+ Class delegateClass = [delegate class ];
66
+
60
67
if (![delegateClass instancesRespondToSelector: swizzledSel]) {
61
- SEL originalSel = @selector (URLSession:task:didCompleteWithError: );
62
68
// If delegate does not exists or delegate does not implement the delegate method, then this
63
69
// request need not be tracked as its completion/failure does not trigger any delegate
64
70
// callbacks.
65
71
if ([delegate respondsToSelector: originalSel]) {
66
- // This is to solve issues where there is a proxy delegate instead of the original instance (e.g. TrustKit).
67
- // It responds YES to `respondsToSelector:` but `class_getInstanceMethod` returns nil.
68
- // We attempt to take the forwarding target for the selector, if it exists.
69
- delegateClass = [[delegate forwardingTargetForSelector: originalSel] class ] ?: delegateClass;
70
72
Class selfClass = [self class ];
71
73
// Double-checked locking to prevent multiple swizzling attempts of the same class.
72
74
@synchronized (selfClass) {
0 commit comments