Skip to content

Commit

Permalink
Keep resident runner connected on iOS when screen locks in debug mode (
Browse files Browse the repository at this point in the history
…flutter#3610)

* Add debug mode background

* Review notes
  • Loading branch information
xster authored Apr 20, 2017
1 parent 414491b commit a5b6489
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 25 additions & 1 deletion shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -26,4 +29,25 @@ - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
}
}

#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

0 comments on commit a5b6489

Please sign in to comment.