我正在为 iOS 应用程序(一个 pod)开发一个框架。我想调酒
application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
使用我的框架中定义的方法。这是我的代码:
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)
}
}
但是当我运行我的代码时,似乎 originalMethod 的值为零,所以
class_getInstanceMethod(appDelegateClass, originalSelector)
返回零。我做错了什么?(请考虑我无权访问 AppDelegate,因为正如我所说,我正在开发一个框架)