I'm developing a framework for iOS apps (a pod). I want to swizzle
application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
with a method defined in my framework. this is my code:
class MyClass {
@objc
func myCustomizedMethod(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// my code
}
private func swizzleDidReceiveRemoteNotification() {
guard let appDelegateClass = object_getClass(UIApplication.shared.delegate) else { return }
let originalSelector = #selector((appDelegateClass as! UIApplicationDelegate).application(_:didReceiveRemoteNotification:fetchCompletionHandler:))
let swizzledSelector = #selector(MyClass.self.myCustomizedMethod(_:didReceiveRemoteNotification:fetchCompletionHandler:))
guard let originalMethod = class_getInstanceMethod(appDelegateClass, originalSelector) else { return }
guard let swizzledMethod = class_getInstanceMethod(MyClass.self, swizzledSelector) else { return }
method_exchangeImplementations(originalMethod, swizzledMethod)
}
}
but when I run my code, it appears that originalMethod has nil value and so
class_getInstanceMethod(appDelegateClass, originalSelector)
returns nil. what I'm doing wrong? (please consider that I don't have access to AppDelegate, because as I said, I'm developing a framework)
That method is optional. If it doesn't exist then you have to add it instead of excanging implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With