[UIKit] handle app lifecycle events in a custom object instead of AppDelegate removes the need to call SDL counterparts manually when custom AppDelegate is used
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
diff --git a/src/video/uikit/SDL_uikitappdelegate.m b/src/video/uikit/SDL_uikitappdelegate.m
index 635ed76..37bd4c7 100644
--- a/src/video/uikit/SDL_uikitappdelegate.m
+++ b/src/video/uikit/SDL_uikitappdelegate.m
@@ -434,43 +434,6 @@ SDL_LoadLaunchImageNamed(NSString *name, int screenh)
/* Do nothing. */
}
-#if !TARGET_OS_TV
-- (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation
-{
- SDL_OnApplicationDidChangeStatusBarOrientation();
-}
-#endif
-
-- (void)applicationWillTerminate:(UIApplication *)application
-{
- SDL_OnApplicationWillTerminate();
-}
-
-- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
-{
- SDL_OnApplicationDidReceiveMemoryWarning();
-}
-
-- (void)applicationWillResignActive:(UIApplication*)application
-{
- SDL_OnApplicationWillResignActive();
-}
-
-- (void)applicationDidEnterBackground:(UIApplication*)application
-{
- SDL_OnApplicationDidEnterBackground();
-}
-
-- (void)applicationWillEnterForeground:(UIApplication*)application
-{
- SDL_OnApplicationWillEnterForeground();
-}
-
-- (void)applicationDidBecomeActive:(UIApplication*)application
-{
- SDL_OnApplicationDidBecomeActive();
-}
-
- (void)sendDropFileForURL:(NSURL *)url
{
NSURL *fileURL = url.filePathURL;
diff --git a/src/video/uikit/SDL_uikitevents.m b/src/video/uikit/SDL_uikitevents.m
index 286db8f..61c57b2 100644
--- a/src/video/uikit/SDL_uikitevents.m
+++ b/src/video/uikit/SDL_uikitevents.m
@@ -41,10 +41,84 @@
static BOOL UIKit_EventPumpEnabled = YES;
+
+@interface SDL_LifecycleObserver : NSObject
+@property (nonatomic, assign) BOOL isObservingNotifications;
+@end
+
+@implementation SDL_LifecycleObserver
+
+- (void)eventPumpChanged
+{
+ NSNotificationCenter *notificationCenter = NSNotificationCenter.defaultCenter;
+ if (UIKit_EventPumpEnabled && !self.isObservingNotifications) {
+ self.isObservingNotifications = YES;
+ [notificationCenter addObserver:self selector:@selector(applicationDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
+ [notificationCenter addObserver:self selector:@selector(applicationWillResignActive) name:UIApplicationWillResignActiveNotification object:nil];
+ [notificationCenter addObserver:self selector:@selector(applicationDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
+ [notificationCenter addObserver:self selector:@selector(applicationWillEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
+ [notificationCenter addObserver:self selector:@selector(applicationWillTerminate) name:UIApplicationWillTerminateNotification object:nil];
+ [notificationCenter addObserver:self selector:@selector(applicationDidReceiveMemoryWarning) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
+#if !TARGET_OS_TV
+ [notificationCenter addObserver:self selector:@selector(applicationDidChangeStatusBarOrientation) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
+#endif
+ } else if (!UIKit_EventPumpEnabled && self.isObservingNotifications) {
+ self.isObservingNotifications = NO;
+ [notificationCenter removeObserver:self];
+ }
+}
+
+- (void)applicationDidBecomeActive
+{
+ SDL_OnApplicationDidBecomeActive();
+}
+
+- (void)applicationWillResignActive
+{
+ SDL_OnApplicationWillResignActive();
+}
+
+- (void)applicationDidEnterBackground
+{
+ SDL_OnApplicationDidEnterBackground();
+}
+
+- (void)applicationWillEnterForeground
+{
+ SDL_OnApplicationWillEnterForeground();
+}
+
+- (void)applicationWillTerminate
+{
+ SDL_OnApplicationWillTerminate();
+}
+
+- (void)applicationDidReceiveMemoryWarning
+{
+ SDL_OnApplicationDidReceiveMemoryWarning();
+}
+
+#if !TARGET_OS_TV
+- (void)applicationDidChangeStatusBarOrientation
+{
+ SDL_OnApplicationDidChangeStatusBarOrientation();
+}
+#endif
+
+@end
+
+
void
SDL_iPhoneSetEventPump(SDL_bool enabled)
{
UIKit_EventPumpEnabled = enabled;
+
+ static SDL_LifecycleObserver *lifecycleObserver;
+ static dispatch_once_t onceToken;
+ dispatch_once(&onceToken, ^{
+ lifecycleObserver = [SDL_LifecycleObserver new];
+ });
+ [lifecycleObserver eventPumpChanged];
}
void