I'm trying to swizzle a private framework's method to perform some custom logic and then would like to call the original implementation.
Code:
class SwizzlingHelper {
private struct Constants {
static let privateFrameworkClassName = "privateFrameworkClassName"
static let swizzledMethodSignature = "swizzledMethodSignature:"
}
static func swizzle() {
let originalSelector = NSSelectorFromString(Constants.swizzledMethodSignature)
if let swizzlingClass: AnyClass = NSClassFromString(Constants.privateFrameworkClassName),
let originalMethod = class_getInstanceMethod(swizzlingClass.self, originalSelector),
let swizzledMethod = class_getClassMethod(SwizzlingHelper.self, #selector(swizzledMethod)) {
method_exchangeImplementations(originalMethod, swizzledMethod)
}
}
@objc
private static func swizzledMethod(_ arg: String) {
// Custom logic
// Call original method here - how?
}
}
I've seen several examples of how swizzling is done by extending a class, and then invoking the original implementation by calling self.originalImplementation()
inside the swizzled method. Since this is a private framework's class, I cannot extend it and hence the SwizzlingHelper
class helps assist with the swizzling. However, there's no access to self
within the swizzled method to call the original implementation.
Any leads will be appreciated. Thanks!