AppDelegateListener.mm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "AppDelegateListener.h"
  2. #define DEFINE_NOTIFICATION(name) extern "C" __attribute__((visibility ("default"))) NSString* const name = @#name;
  3. DEFINE_NOTIFICATION(kUnityDidRegisterForRemoteNotificationsWithDeviceToken);
  4. DEFINE_NOTIFICATION(kUnityDidFailToRegisterForRemoteNotificationsWithError);
  5. DEFINE_NOTIFICATION(kUnityDidReceiveRemoteNotification);
  6. DEFINE_NOTIFICATION(kUnityDidReceiveLocalNotification);
  7. DEFINE_NOTIFICATION(kUnityOnOpenURL);
  8. #undef DEFINE_NOTIFICATION
  9. void UnityRegisterAppDelegateListener(id<AppDelegateListener> obj)
  10. {
  11. #define REGISTER_SELECTOR(sel, notif_name) \
  12. if([obj respondsToSelector:sel]) \
  13. [[NSNotificationCenter defaultCenter] addObserver:obj \
  14. selector:sel \
  15. name:notif_name \
  16. object:nil \
  17. ]; \
  18. UnityRegisterLifeCycleListener(obj);
  19. REGISTER_SELECTOR(@selector(didRegisterForRemoteNotificationsWithDeviceToken:), kUnityDidRegisterForRemoteNotificationsWithDeviceToken);
  20. REGISTER_SELECTOR(@selector(didFailToRegisterForRemoteNotificationsWithError:), kUnityDidFailToRegisterForRemoteNotificationsWithError);
  21. REGISTER_SELECTOR(@selector(didReceiveRemoteNotification:), kUnityDidReceiveRemoteNotification);
  22. REGISTER_SELECTOR(@selector(didReceiveLocalNotification:), kUnityDidReceiveLocalNotification);
  23. REGISTER_SELECTOR(@selector(onOpenURL:), kUnityOnOpenURL);
  24. REGISTER_SELECTOR(@selector(applicationDidReceiveMemoryWarning:), UIApplicationDidReceiveMemoryWarningNotification);
  25. REGISTER_SELECTOR(@selector(applicationSignificantTimeChange:), UIApplicationSignificantTimeChangeNotification);
  26. #if !PLATFORM_TVOS
  27. REGISTER_SELECTOR(@selector(applicationWillChangeStatusBarFrame:), UIApplicationWillChangeStatusBarFrameNotification);
  28. REGISTER_SELECTOR(@selector(applicationWillChangeStatusBarOrientation:), UIApplicationWillChangeStatusBarOrientationNotification);
  29. #endif
  30. #undef REGISTER_SELECTOR
  31. }
  32. void UnityUnregisterAppDelegateListener(id<AppDelegateListener> obj)
  33. {
  34. UnityUnregisterLifeCycleListener(obj);
  35. [[NSNotificationCenter defaultCenter] removeObserver: obj name: kUnityDidRegisterForRemoteNotificationsWithDeviceToken object: nil];
  36. [[NSNotificationCenter defaultCenter] removeObserver: obj name: kUnityDidFailToRegisterForRemoteNotificationsWithError object: nil];
  37. [[NSNotificationCenter defaultCenter] removeObserver: obj name: kUnityDidReceiveRemoteNotification object: nil];
  38. [[NSNotificationCenter defaultCenter] removeObserver: obj name: kUnityDidReceiveLocalNotification object: nil];
  39. [[NSNotificationCenter defaultCenter] removeObserver: obj name: kUnityOnOpenURL object: nil];
  40. [[NSNotificationCenter defaultCenter] removeObserver: obj name: UIApplicationDidReceiveMemoryWarningNotification object: nil];
  41. [[NSNotificationCenter defaultCenter] removeObserver: obj name: UIApplicationSignificantTimeChangeNotification object: nil];
  42. #if !PLATFORM_TVOS
  43. [[NSNotificationCenter defaultCenter] removeObserver: obj name: UIApplicationWillChangeStatusBarFrameNotification object: nil];
  44. [[NSNotificationCenter defaultCenter] removeObserver: obj name: UIApplicationWillChangeStatusBarOrientationNotification object: nil];
  45. #endif
  46. }