replace swizzle method_exchangeImplementations with method_setImplementation (#7991)

This commit is contained in:
Elias Nahum 2024-06-11 05:14:53 +08:00 committed by GitHub
parent b966e1f356
commit f0e80aa7eb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 45 additions and 8 deletions

View file

@ -16,6 +16,10 @@
NSString *const ReplyActionID = @"REPLY_ACTION";
typedef void (*SendReplyCompletionHandlerIMP)(id, SEL, UNNotificationResponse *, void (^)(void));
static SendReplyCompletionHandlerIMP originalSendReplyCompletionHandlerImplementation = NULL;
@implementation RNNotificationEventHandler (HandleReplyAction)
- (RNNotificationCenter *)notificationCenter{
@ -37,7 +41,16 @@ NSString *const ReplyActionID = @"REPLY_ACTION";
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
method_exchangeImplementations(originalMethod, swizzledMethod);
// Get the implementation of the swizzled method
IMP swizzledImplementation = method_getImplementation(swizzledMethod);
// Get the original implementation
IMP originalImplementation = method_getImplementation(originalMethod);
// Set the original method's implementation to the swizzled method's implementation
method_setImplementation(originalMethod, swizzledImplementation);
originalSendReplyCompletionHandlerImplementation = (SendReplyCompletionHandlerIMP)originalImplementation;
});
}
@ -140,7 +153,7 @@ NSString *const ReplyActionID = @"REPLY_ACTION";
if ([response.actionIdentifier isEqualToString:ReplyActionID]) {
[self sendReply:response completionHandler:completionHandler];
} else {
[self handleReplyAction_didReceiveNotificationResponse:response completionHandler:completionHandler];
originalSendReplyCompletionHandlerImplementation(self, @selector(sendReply:completionHandler:), response, completionHandler);
}
}

View file

@ -8,6 +8,9 @@
@import react_native_network_client;
#import <objc/runtime.h>
typedef id (*InitWithRequestInSessionOptionsContextIMP)(id, SEL, NSURLRequest *, NSURLSession *, SDWebImageDownloaderOptions *, id);
typedef void (*URLSessionTaskDidReceiveChallengeIMP)(id, SEL, NSURLSession *, NSURLSessionTask *, NSURLAuthenticationChallenge *, void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *));
@implementation SDWebImageDownloaderOperation (Swizzle)
+ (void) load {
@ -18,6 +21,9 @@
});
}
static InitWithRequestInSessionOptionsContextIMP originalInitWithRequestInSessionOptionsContextImplementation = NULL;
static URLSessionTaskDidReceiveChallengeIMP originalURLSessionTaskDidReceiveChallengeImplementation = NULL;
+ (void) swizzleInitMethod {
Class class = [self class];
@ -27,7 +33,16 @@
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
method_exchangeImplementations(originalMethod, swizzledMethod);
// Get the implementation of the swizzled method
IMP swizzledImplementation = method_getImplementation(swizzledMethod);
// Get the original implementation
IMP originalImplementation = method_getImplementation(originalMethod);
// Set the original method's implementation to the swizzled method's implementation
method_setImplementation(originalMethod, swizzledImplementation);
originalInitWithRequestInSessionOptionsContextImplementation = (InitWithRequestInSessionOptionsContextIMP)originalImplementation;
}
@ -40,7 +55,16 @@
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
method_exchangeImplementations(originalMethod, swizzledMethod);
// Get the implementation of the swizzled method
IMP swizzledImplementation = method_getImplementation(swizzledMethod);
// Get the original implementation
IMP originalImplementation = method_getImplementation(originalMethod);
// Set the original method's implementation to the swizzled method's implementation
method_setImplementation(originalMethod, swizzledImplementation);
originalURLSessionTaskDidReceiveChallengeImplementation = (URLSessionTaskDidReceiveChallengeIMP)originalImplementation;
}
#pragma mark - Method Swizzling
@ -56,14 +80,14 @@
// our BearerAuthenticationAdapter.
NSURLSessionConfiguration *configuration = [nativeClientSessionManager getSessionConfigurationFor:sessionBaseUrl];
NSURLSession *newSession = [NSURLSession sessionWithConfiguration:configuration
delegate:self
delegate:self
delegateQueue:session.delegateQueue];
NSURLRequest *authorizedRequest = [BearerAuthenticationAdapter addAuthorizationBearerTokenTo:request withSessionBaseUrlString:sessionBaseUrl.absoluteString];
return [self swizzled_initWithRequest:authorizedRequest inSession:newSession options:options context:context];
return originalInitWithRequestInSessionOptionsContextImplementation(self, @selector(initWithRequest:inSession:options:context:), authorizedRequest, session, &options, context);
}
return [self swizzled_initWithRequest:request inSession:session options:options context:context];
return originalInitWithRequestInSessionOptionsContextImplementation(self, @selector(initWithRequest:inSession:options:context:), request, session, &options, context);
}
@ -95,7 +119,7 @@
return;
}
[self swizzled_URLSession:session task:task didReceiveChallenge:challenge completionHandler:completionHandler];
originalURLSessionTaskDidReceiveChallengeImplementation(self, @selector(URLSession:task:didReceiveChallenge:completionHandler:), session, task, challenge, completionHandler);
}
@end