Skip to content

Commit

Permalink
💫 Update: UIViewController+Swizzling
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed Apr 26, 2023
1 parent f3c2fe9 commit cd86c6d
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions ios/src_library/Extensions/UIViewController+Swizzling.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@

import Foundation

// TODO: Move to `RNIUtilities`
extension RNIUtilities {
static func swizzleExchangeMethods(
defaultSelector: Selector,
newSelector: Selector,
forClass class: AnyClass
) {
let defaultInstace =
class_getInstanceMethod(`class`.self, defaultSelector);

let newInstance =
class_getInstanceMethod(`class`.self, newSelector);

guard let defaultInstance = defaultInstace,
let newInstance = newInstance
else { return };

method_exchangeImplementations(defaultInstance, newInstance);
};
};


fileprivate class RNIModalWrapperMap {
static let instanceMap = NSMapTable<
Expand Down Expand Up @@ -200,25 +221,6 @@ extension UIViewController {
};
};

// TODO: Move to `RNIUtilities`
static func swizzleExchangeMethods(
defaultSelector: Selector,
newSelector: Selector,
forClass class: AnyClass
) {
let defaultInstace =
class_getInstanceMethod(`class`.self, defaultSelector);

let newInstance =
class_getInstanceMethod(`class`.self, newSelector);

guard let defaultInstance = defaultInstace,
let newInstance = newInstance
else { return };

method_exchangeImplementations(defaultInstance, newInstance);
};

internal static func swizzleMethods() {
guard RNIModalSwizzling.shouldEnableSwizzling else { return };

Expand All @@ -229,13 +231,13 @@ extension UIViewController {
);
#endif

self.swizzleExchangeMethods(
RNIUtilities.swizzleExchangeMethods(
defaultSelector: #selector(Self.present(_:animated:completion:)),
newSelector: #selector(Self._swizzled_present(_:animated:completion:)),
forClass: UIViewController.self
);

self.swizzleExchangeMethods(
RNIUtilities.swizzleExchangeMethods(
defaultSelector: #selector(Self.dismiss(animated:completion:)),
newSelector: #selector(Self._swizzled_dismiss(animated:completion:)),
forClass: UIViewController.self
Expand Down

0 comments on commit cd86c6d

Please sign in to comment.