diff --git a/doc/classes/MainLoop.xml b/doc/classes/MainLoop.xml index fa0c2c449..9631c1390 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 672e13e83..e985841eb 100644 --- a/doc/classes/Node.xml +++ b/doc/classes/Node.xml @@ -1085,11 +1085,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 3c1a77f26..fe70491b4 100644 --- a/platform/iphone/app_delegate.mm +++ b/platform/iphone/app_delegate.mm @@ -175,6 +175,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 236567ba4..cd55d7a01 100644 --- a/platform/iphone/os_iphone.h +++ b/platform/iphone/os_iphone.h @@ -194,6 +194,9 @@ public: void on_focus_out(); void on_focus_in(); + + void on_enter_background(); + void on_exit_background(); }; #endif // OS_IPHONE_H diff --git a/platform/iphone/os_iphone.mm b/platform/iphone/os_iphone.mm index 6a78e9518..53b334312 100644 --- a/platform/iphone/os_iphone.mm +++ b/platform/iphone/os_iphone.mm @@ -802,4 +802,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