[iOS] Add APP_PAUSED and APP_RESUMED MainLoop notifications

This commit is contained in:
Zach Coleman 2023-11-22 11:43:40 -05:00
parent e96c42f0e1
commit ffe86eef04
5 changed files with 37 additions and 4 deletions

View file

@ -201,11 +201,12 @@
</constant>
<constant name="NOTIFICATION_APP_RESUMED" value="1014">
Notification received from the OS when the app is resumed.
Specific to the Android platform.
Specific to mobile platforms.
</constant>
<constant name="NOTIFICATION_APP_PAUSED" value="1015">
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.
</constant>
</constants>
</class>

View file

@ -928,11 +928,12 @@
</constant>
<constant name="NOTIFICATION_APP_RESUMED" value="1014">
Notification received from the OS when the app is resumed.
Specific to the Android platform.
Specific to mobile platforms.
</constant>
<constant name="NOTIFICATION_APP_PAUSED" value="1015">
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.
</constant>
<constant name="PAUSE_MODE_INHERIT" value="0" enum="PauseMode">
Inherits pause mode from the node's parent. For the root node, it is equivalent to [constant PAUSE_MODE_STOP]. Default.

View file

@ -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;
}

View file

@ -204,6 +204,9 @@ public:
void on_focus_out();
void on_focus_in();
void on_enter_background();
void on_exit_background();
};
#endif // IPHONE_ENABLED

View file

@ -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