From 0acc6065680d734d0411bf00df573f089a06aee3 Mon Sep 17 00:00:00 2001 From: Elias Nahum Date: Wed, 5 Jun 2024 11:50:13 +0800 Subject: [PATCH] replace swizzle method_exchangeImplementations with method_setImplementation --- ...tificationEventHandler+HandleReplyAction.m | 17 +++++++-- .../SDWebImageDownloaderOperation+Swizzle.m | 36 +++++++++++++++---- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/ios/Mattermost/RNNotificationEventHandler+HandleReplyAction.m b/ios/Mattermost/RNNotificationEventHandler+HandleReplyAction.m index 811c255459b..ab43b048a1d 100644 --- a/ios/Mattermost/RNNotificationEventHandler+HandleReplyAction.m +++ b/ios/Mattermost/RNNotificationEventHandler+HandleReplyAction.m @@ -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 @@ + (void)load { 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 @@ - (void)handleReplyAction_didReceiveNotificationResponse:(UNNotificationResponse if ([response.actionIdentifier isEqualToString:ReplyActionID]) { [self sendReply:response completionHandler:completionHandler]; } else { - [self handleReplyAction_didReceiveNotificationResponse:response completionHandler:completionHandler]; + originalSendReplyCompletionHandlerImplementation(self, @selector(sendReply:completionHandler:), response, completionHandler); } } diff --git a/ios/Mattermost/SDWebImageDownloaderOperation+Swizzle.m b/ios/Mattermost/SDWebImageDownloaderOperation+Swizzle.m index fdf9238c37e..9baa2022f5b 100644 --- a/ios/Mattermost/SDWebImageDownloaderOperation+Swizzle.m +++ b/ios/Mattermost/SDWebImageDownloaderOperation+Swizzle.m @@ -8,6 +8,9 @@ @import react_native_network_client; #import +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 @@ + (void) load { }); } +static InitWithRequestInSessionOptionsContextIMP originalInitWithRequestInSessionOptionsContextImplementation = NULL; +static URLSessionTaskDidReceiveChallengeIMP originalURLSessionTaskDidReceiveChallengeImplementation = NULL; + + (void) swizzleInitMethod { Class class = [self class]; @@ -27,7 +33,16 @@ + (void) swizzleInitMethod { 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 @@ + (void) swizzleURLSessionTaskDelegateMethod { 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 @@ - (nonnull instancetype)swizzled_initWithRequest:(NSURLRequest *)request inSessi // 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 @@ - (void)swizzled_URLSession:(NSURLSession *)session task:(NSURLSessionTask *)tas return; } - [self swizzled_URLSession:session task:task didReceiveChallenge:challenge completionHandler:completionHandler]; + originalURLSessionTaskDidReceiveChallengeImplementation(self, @selector(URLSession:task:didReceiveChallenge:completionHandler:), session, task, challenge, completionHandler); } @end