LifeCycleListener.mm 1.9 KB

1234567891011121314151617181920212223242526272829303132
  1. #include "LifeCycleListener.h"
  2. void UnityRegisterLifeCycleListener(id<LifeCycleListener> obj)
  3. {
  4. #define REGISTER_SELECTOR(sel, notif_name) \
  5. if([obj respondsToSelector:sel]) \
  6. [[NSNotificationCenter defaultCenter] addObserver:obj \
  7. selector:sel \
  8. name:notif_name \
  9. object:nil \
  10. ]; \
  11. REGISTER_SELECTOR(@selector(didFinishLaunching:), UIApplicationDidFinishLaunchingNotification);
  12. REGISTER_SELECTOR(@selector(didBecomeActive:), UIApplicationDidBecomeActiveNotification);
  13. REGISTER_SELECTOR(@selector(willResignActive:), UIApplicationWillResignActiveNotification);
  14. REGISTER_SELECTOR(@selector(didEnterBackground:), UIApplicationDidEnterBackgroundNotification);
  15. REGISTER_SELECTOR(@selector(willEnterForeground:), UIApplicationWillEnterForegroundNotification);
  16. REGISTER_SELECTOR(@selector(willTerminate:), UIApplicationWillTerminateNotification);
  17. #undef REGISTER_SELECTOR
  18. }
  19. void UnityUnregisterLifeCycleListener(id<LifeCycleListener> obj)
  20. {
  21. [[NSNotificationCenter defaultCenter] removeObserver: obj name: UIApplicationDidFinishLaunchingNotification object: nil];
  22. [[NSNotificationCenter defaultCenter] removeObserver: obj name: UIApplicationDidBecomeActiveNotification object: nil];
  23. [[NSNotificationCenter defaultCenter] removeObserver: obj name: UIApplicationWillResignActiveNotification object: nil];
  24. [[NSNotificationCenter defaultCenter] removeObserver: obj name: UIApplicationDidEnterBackgroundNotification object: nil];
  25. [[NSNotificationCenter defaultCenter] removeObserver: obj name: UIApplicationWillEnterForegroundNotification object: nil];
  26. [[NSNotificationCenter defaultCenter] removeObserver: obj name: UIApplicationWillTerminateNotification object: nil];
  27. }