Skip to content

Commit

Permalink
replace swizzle method_exchangeImplementations with method_setImpleme…
Browse files Browse the repository at this point in the history
…ntation
  • Loading branch information
enahum committed Jun 5, 2024
1 parent f46ddb4 commit 0acc606
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
17 changes: 15 additions & 2 deletions ios/Mattermost/RNNotificationEventHandler+HandleReplyAction.m
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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;
});
}

Expand Down Expand Up @@ -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);
}
}

Expand Down
36 changes: 30 additions & 6 deletions ios/Mattermost/SDWebImageDownloaderOperation+Swizzle.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -18,6 +21,9 @@ + (void) load {
});
}

static InitWithRequestInSessionOptionsContextIMP originalInitWithRequestInSessionOptionsContextImplementation = NULL;
static URLSessionTaskDidReceiveChallengeIMP originalURLSessionTaskDidReceiveChallengeImplementation = NULL;

+ (void) swizzleInitMethod {
Class class = [self class];

Expand All @@ -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;

}

Expand All @@ -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
Expand All @@ -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);
}


Expand Down Expand Up @@ -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

0 comments on commit 0acc606

Please sign in to comment.