From ffe86eef04f4e44d6a974dcade45527364265556 Mon Sep 17 00:00:00 2001 From: Zach Coleman Date: Wed, 22 Nov 2023 11:43:40 -0500 Subject: [PATCH] [iOS] Add APP_PAUSED and APP_RESUMED MainLoop notifications --- doc/classes/MainLoop.xml | 5 +++-- doc/classes/Node.xml | 5 +++-- platform/iphone/app_delegate.mm | 8 ++++++++ platform/iphone/os_iphone.h | 3 +++ platform/iphone/os_iphone.mm | 20 ++++++++++++++++++++ 5 files changed, 37 insertions(+), 4 deletions(-) diff --git a/doc/classes/MainLoop.xml b/doc/classes/MainLoop.xml index 3092c822019..daeb1793f7e 100644 --- a/doc/classes/MainLoop.xml +++ b/doc/classes/MainLoop.xml @@ -201,11 +201,12 @@ Notification received from the OS when the app is resumed. - Specific to the Android platform. + Specific to mobile platforms. Notification received from the OS when the app is paused. - Specific to the Android platform. + Specific to mobile platforms. + [b]Note:[/b] On iOS, you only have approximately 5 seconds to finish a task started by this signal. If you go over this allotment, iOS will kill the app instead of pausing it. diff --git a/doc/classes/Node.xml b/doc/classes/Node.xml index 827accb47a6..ad3f7e2aa59 100644 --- a/doc/classes/Node.xml +++ b/doc/classes/Node.xml @@ -928,11 +928,12 @@ Notification received from the OS when the app is resumed. - Specific to the Android platform. + Specific to mobile platforms. Notification received from the OS when the app is paused. - Specific to the Android platform. + Specific to mobile platforms. + [b]Note:[/b] On iOS, you only have approximately 5 seconds to finish a task started by this signal. If you go over this allotment, iOS will kill the app instead of pausing it. Inherits pause mode from the node's parent. For the root node, it is equivalent to [constant PAUSE_MODE_STOP]. Default. diff --git a/platform/iphone/app_delegate.mm b/platform/iphone/app_delegate.mm index b0540f6a7fc..1dcd0b01945 100644 --- a/platform/iphone/app_delegate.mm +++ b/platform/iphone/app_delegate.mm @@ -145,6 +145,14 @@ static ViewController *mainViewController = nil; OSIPhone::get_singleton()->on_focus_in(); } +- (void)applicationDidEnterBackground:(UIApplication *)application { + OSIPhone::get_singleton()->on_enter_background(); +} + +- (void)applicationWillEnterForeground:(UIApplication *)application { + OSIPhone::get_singleton()->on_exit_background(); +} + - (void)dealloc { self.window = nil; } diff --git a/platform/iphone/os_iphone.h b/platform/iphone/os_iphone.h index 013c3411e5f..3c18578b0a2 100644 --- a/platform/iphone/os_iphone.h +++ b/platform/iphone/os_iphone.h @@ -204,6 +204,9 @@ public: void on_focus_out(); void on_focus_in(); + + void on_enter_background(); + void on_exit_background(); }; #endif // IPHONE_ENABLED diff --git a/platform/iphone/os_iphone.mm b/platform/iphone/os_iphone.mm index 7b10d92c9b2..100590bfce5 100644 --- a/platform/iphone/os_iphone.mm +++ b/platform/iphone/os_iphone.mm @@ -861,4 +861,24 @@ void OSIPhone::on_focus_in() { } } +void OSIPhone::on_enter_background() { + // Do not check for is_focused, because on_focus_out will always be fired first by applicationWillResignActive. + + if (get_main_loop()) { + get_main_loop()->notification(MainLoop::NOTIFICATION_APP_PAUSED); + } + + on_focus_out(); +} + +void OSIPhone::on_exit_background() { + if (!is_focused) { + on_focus_in(); + + if (get_main_loop()) { + get_main_loop()->notification(MainLoop::NOTIFICATION_APP_RESUMED); + } + } +} + #endif