AppDelegateListener.mm 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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(kUnityOnOpenURL);
  7. DEFINE_NOTIFICATION(kUnityWillFinishLaunchingWithOptions);
  8. DEFINE_NOTIFICATION(kUnityHandleEventsForBackgroundURLSession);
  9. #undef DEFINE_NOTIFICATION
  10. void UnityRegisterAppDelegateListener(id<AppDelegateListener> obj)
  11. {
  12. #define REGISTER_SELECTOR(sel, notif_name) \
  13. if([obj respondsToSelector:sel]) \
  14. [[NSNotificationCenter defaultCenter] addObserver:obj \
  15. selector:sel \
  16. name:notif_name \
  17. object:nil \
  18. ]; \
  19. UnityRegisterLifeCycleListener(obj);
  20. REGISTER_SELECTOR(@selector(onOpenURL:), kUnityOnOpenURL);
  21. REGISTER_SELECTOR(@selector(applicationDidReceiveMemoryWarning:), UIApplicationDidReceiveMemoryWarningNotification);
  22. REGISTER_SELECTOR(@selector(applicationSignificantTimeChange:), UIApplicationSignificantTimeChangeNotification);
  23. #if !PLATFORM_TVOS && !PLATFORM_VISIONOS
  24. REGISTER_SELECTOR(@selector(applicationWillChangeStatusBarFrame:), UIApplicationWillChangeStatusBarFrameNotification);
  25. REGISTER_SELECTOR(@selector(applicationWillChangeStatusBarOrientation:), UIApplicationWillChangeStatusBarOrientationNotification);
  26. #endif
  27. REGISTER_SELECTOR(@selector(applicationWillFinishLaunchingWithOptions:), kUnityWillFinishLaunchingWithOptions);
  28. REGISTER_SELECTOR(@selector(onHandleEventsForBackgroundURLSession:), kUnityHandleEventsForBackgroundURLSession);
  29. #undef REGISTER_SELECTOR
  30. }
  31. void UnityUnregisterAppDelegateListener(id<AppDelegateListener> obj)
  32. {
  33. UnityUnregisterLifeCycleListener(obj);
  34. [[NSNotificationCenter defaultCenter] removeObserver: obj name: kUnityOnOpenURL object: nil];
  35. [[NSNotificationCenter defaultCenter] removeObserver: obj name: UIApplicationDidReceiveMemoryWarningNotification object: nil];
  36. [[NSNotificationCenter defaultCenter] removeObserver: obj name: UIApplicationSignificantTimeChangeNotification object: nil];
  37. #if !PLATFORM_TVOS && !PLATFORM_VISIONOS
  38. [[NSNotificationCenter defaultCenter] removeObserver: obj name: UIApplicationWillChangeStatusBarFrameNotification object: nil];
  39. [[NSNotificationCenter defaultCenter] removeObserver: obj name: UIApplicationWillChangeStatusBarOrientationNotification object: nil];
  40. #endif
  41. }