Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Intercept Action Swizzling in Swift
@objc static func swizzleMethod() {
guard self == UIApplication.self else {
let sendActionSelector = #selector(self.sendAction(_:to:from:for:))
let swizzleActionSelector = #selector(self.interceptAction(_:to:from:for:))
let sendActionMethod = class_getInstanceMethod(self, sendActionSelector)
let swizzleActionMethod = class_getInstanceMethod(self, swizzleActionSelector)
method_exchangeImplementations(sendActionMethod!, swizzleActionMethod!)
@objc func interceptAction(_ action: Selector,
to target: Any?,
from sender: Any?,
for event: UIEvent?) -> Bool {
DispatchQueue.main.async {
self.captureSenderInfo(sender: sender)
return self.interceptAction(action, to: target, from: sender, for: event)
internal func captureSenderInfo(sender: Any?) {
if let button = sender as? UIButton {
logButton(T: button)
} else if let barButton = sender as? UIBarButtonItem {
logBarButton(T: barButton)
} else if let pageControl = sender as? UIPageControl {
logPageControl(T: pageControl)
func logButton<T>(T: T) where T: UIButton {
func logBarButton<T>(T: T) where T: UIBarButtonItem {
func logPageControl<T>(T: T) where T: UIPageControl {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment