2

I have a permissions framework in Swift that needs a push notification permission delegate callback. The push notification callback resides in the App Delegate, so my framework won't have access to each app's delegate. I'm thinking I could use method swizzling to intercept the methods (i.e. didRegisterForRemoteNotificationsWithDeviceToken).

To test, I have:

let method1 = class_getInstanceMethod(UIApplication.shared.delegate as! AnyClass!, Selector.init(stringLiteral: "applicationWillEnterForeground:"))
let method2 = class_getInstanceMethod(self.classForCoder, Selector.init(stringLiteral: "testerFunc"))
if let method1 = method1, let method2 = method2 {
   method_exchangeImplementations(method1, method2)
}

Any ideas?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Zach Lucas
  • 1,631
  • 5
  • 15
  • 29
  • 1
    This is the best tutorial on how to swizzle. http://nshipster.com/method-swizzling/ However swizzling is what you think the solution to your actual problem is, why not ask about the best solution to your problem rather than ask about swizzling? But its not explained enough at the moment to comment on it. – Gruntcakes Nov 16 '16 at 22:11
  • If I were in a standalone app instead of a framework, I could do something like: ```if let myDelegate = UIApplication.shared.delegate as? AppDelegate { let method1 = class_getInstanceMethod(myDelegate.classForCoder, Selector.init(stringLiteral: "applicationWillEnterForeground:")) }```, but I don't have access to `AppDelegate` class since I'm in a framework that will be added to n number of apps (via cocoapods) – Zach Lucas Nov 16 '16 at 22:17
  • Regarding the code in your comment, a framework can still register directly for an applicationWillEnterForeground event if it just wants to know when the app goes into the foreground. However applicationWillEnterForeground has got nothing to do with pushes and push permissions, so don't know the relation between the question and the comment code. If your framework needs to know when the app has received a push or a push permission it can easily be done simply via a callback or a block or even just a direct call from the app to the framework. It sounds like you're over complicating things. – Gruntcakes Nov 16 '16 at 22:30
  • How would I get a callback to a method like `didRegisterForRemoteNotificationsWithDeviceToken:` in my framework without having each app that uses the framework implement some sort of callback or block inside their own `AppDelegate`'s `didRegisterForRemoteNotificationsWithDeviceToken` method? – Zach Lucas Nov 16 '16 at 22:33
  • So you want to totally decouple the apps from the classes/API in the framework? Your apps don't use any classes/API in the framework? Or they do, but you want to minimize that to a bare bones amount? – Gruntcakes Nov 16 '16 at 22:39
  • Incidentally, here's a related past question on swizzling http://stackoverflow.com/questions/20483159/can-you-swizzle-applicationdidreceiveremotenotification – Gruntcakes Nov 16 '16 at 22:42

0 Answers0