【发布时间】:2020-01-10 14:05:15
【问题描述】:
我正在为 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,因为正如我所说,我正在开发一个框架)
【问题讨论】:
-
swizzling didReceiveRemoteNotification 的目的是什么?它将提供或克服任何问题的任何附加功能?请提供更多信息。
-
我想在我的代码中做我需要的一切,所以任何使用我的框架的人,只使用 pod install。更清楚的是,我想对收到的通知进行一些分析(例如交付,...)。我想让我的框架使用尽可能简单。
标签: ios swift swizzling method-swizzling swizzle