Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to swizzle didReceiveRemoteNotification of AppDelegate from framework

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)

like image 958
Jafar Khoshtabiat Avatar asked Jan 25 '23 20:01

Jafar Khoshtabiat


1 Answers

That method is optional. If it doesn't exist then you have to add it instead of excanging implementation.

like image 170
Cyberbeni Avatar answered Jan 29 '23 10:01

Cyberbeni