Skip to content

Commit 4730651

Browse files
authored
Merge pull request google#1 from LeoNatan/fix-further-swizzling-woes
Fix further NSURLSession swizzling woes
2 parents 4a544a8 + b9c2eb9 commit 4730651

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

EarlGrey/Additions/NSURLSession+GREYAdditions.m

+10-8
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,21 @@ - (NSURLSessionDataTask *)greyswizzled_dataTaskWithRequest:(NSURLRequest *)reque
5454
// Swizzle the session delegate class if not yet done.
5555
id delegate = self.delegate;
5656
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+
6067
if (![delegateClass instancesRespondToSelector:swizzledSel]) {
61-
SEL originalSel = @selector(URLSession:task:didCompleteWithError:);
6268
// If delegate does not exists or delegate does not implement the delegate method, then this
6369
// request need not be tracked as its completion/failure does not trigger any delegate
6470
// callbacks.
6571
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;
7072
Class selfClass = [self class];
7173
// Double-checked locking to prevent multiple swizzling attempts of the same class.
7274
@synchronized(selfClass) {

0 commit comments

Comments
 (0)