【问题标题】:how to swizzle didReceiveRemoteNotification of AppDelegate from framework如何从框架中调配 AppDelegate 的 didReceiveRemoteNotification
【发布时间】: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


【解决方案1】:

这是对我有用的代码:

class MyClass {
    @objc
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        // my code
    }

    private func swizzleDidReceiveRemoteNotification() {
        let appDelegate = UIApplication.shared.delegate
        let appDelegateClass = object_getClass(appDelegate)

        let originalSelector = #selector(UIApplicationDelegate.application(_:didReceiveRemoteNotification:fetchCompletionHandler:))
        let swizzledSelector = #selector(MyClass.self.application(_:didReceiveRemoteNotification:fetchCompletionHandler:))

        guard let swizzledMethod = class_getInstanceMethod(MyClass.self, swizzledSelector) else {
            return
        }

        if let originalMethod = class_getInstanceMethod(appDelegateClass, originalSelector)  {
            // exchange implementation
            method_exchangeImplementations(originalMethod, swizzledMethod)
        } else {
            // add implementation
            class_addMethod(appDelegateClass, swizzledSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
        }
    }
}

【讨论】:

    【解决方案2】:

    该方法是可选的。如果它不存在,那么您必须添加它而不是扩展实现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-09
      • 2018-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多