From 7900afc2fa03880fd88ea218cbed94c71fc74d44 Mon Sep 17 00:00:00 2001 From: xster Date: Wed, 19 Apr 2017 18:00:06 -0700 Subject: [PATCH] Keep resident runner connected on iOS when screen locks in debug mode (flutter/engine#3610) * Add debug mode background * Review notes --- .../framework/Headers/FlutterAppDelegate.h | 2 ++ .../framework/Source/FlutterAppDelegate.mm | 26 ++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterAppDelegate.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterAppDelegate.h index 967c55745b7..90a4f344edb 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterAppDelegate.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterAppDelegate.h @@ -15,6 +15,8 @@ * This class provides the following behaviors: * * Status bar touches are forwarded to the key window's root view * FlutterViewController, in order to trigger scroll to top. + * * Keeps the Flutter connection open in debug mode when the phone screen + * locks. * * App delegates for Flutter applications are *not* required to inherit from * this class. Developers of custom app delegate classes should copy and paste diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm index 20bde7589f9..50627c8e3ee 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm @@ -4,8 +4,11 @@ #include "flutter/shell/platform/darwin/ios/framework/Headers/FlutterAppDelegate.h" #include "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h" +#include "lib/ftl/logging.h" -@implementation FlutterAppDelegate +@implementation FlutterAppDelegate { + UIBackgroundTaskIdentifier _debugBackgroundTask; +} // Returns the key window's rootViewController, if it's a FlutterViewController. // Otherwise, returns nil. @@ -26,4 +29,25 @@ } } +#if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG +- (void)applicationDidEnterBackground:(UIApplication *)application { + // The following keeps the Flutter session alive when the device screen locks + // in debug mode. It allows continued use of features like hot reload and + // taking screenshots once the device unlocks again. + // + // Note the name is not an identifier and multiple instances can exist. + _debugBackgroundTask = [application beginBackgroundTaskWithName:@"Flutter debug task" + expirationHandler:^{ + FTL_LOG(WARNING) << "\nThe OS has terminated the Flutter debug connection for being " + "inactive in the background for too long.\n\n" + "There are no errors with your Flutter application.\n\n" + "To reconnect, launch your application again via 'flutter run"; + }]; +} + +- (void)applicationWillEnterForeground:(UIApplication *)application { + [application endBackgroundTask: _debugBackgroundTask]; +} +#endif // FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG + @end