diff --git a/shell/platform/darwin/ios/framework/Headers/FlutterEngine.h b/shell/platform/darwin/ios/framework/Headers/FlutterEngine.h index ce0385f6672fb..193a9cc8e8be7 100644 --- a/shell/platform/darwin/ios/framework/Headers/FlutterEngine.h +++ b/shell/platform/darwin/ios/framework/Headers/FlutterEngine.h @@ -18,6 +18,12 @@ NS_ASSUME_NONNULL_BEGIN +/** + * The dart entrypoint that is associated with `main()`. This is to be used as an argument to the + * `runWithEntrypoint*` methods. + */ +extern NSString* const FlutterDefaultDartEntrypoint; + /** * The FlutterEngine class coordinates a single instance of execution for a * `FlutterDartProject`. It may have zero or one `FlutterViewController` at a @@ -41,6 +47,26 @@ NS_ASSUME_NONNULL_BEGIN */ FLUTTER_EXPORT @interface FlutterEngine : NSObject + +/** + * Initialize this FlutterEngine. + * + * The engine will execute the project located in the bundle with the identifier + * "io.flutter.flutter.app" (the default for Flutter projects). + * + * A newly initialized engine will not run until either `-runWithEntrypoint:` or + * `-runWithEntrypoint:libraryURI:` is called. + * + * FlutterEngine created with this method will have allowHeadlessExecution set to `YES`. + * This means that the engine will continue to run regardless of whether a `FlutterViewController` + * is attached to it or not, until `-destroyContext:` is called or the process finishes. + * + * @param labelPrefix The label prefix used to identify threads for this instance. Should + * be unique across FlutterEngine instances, and is used in instrumentation to label + * the threads used by this FlutterEngine. + */ +- (instancetype)initWithName:(NSString*)labelPrefix; + /** * Initialize this FlutterEngine with a `FlutterDartProject`. * @@ -91,6 +117,17 @@ FLUTTER_EXPORT + (instancetype)new NS_UNAVAILABLE; +/** + * Runs a Dart program on an Isolate from the main Dart library (i.e. the library that + * contains `main()`), using `main()` as the entrypoint (the default for Flutter projects). + * + * The first call to this method will create a new Isolate. Subsequent calls will return + * immediately. + * + * @return YES if the call succeeds in creating and running a Flutter Engine instance; NO otherwise. + */ +- (BOOL)run; + /** * Runs a Dart program on an Isolate from the main Dart library (i.e. the library that * contains `main()`). @@ -99,10 +136,10 @@ FLUTTER_EXPORT * immediately. * * @param entrypoint The name of a top-level function from the same Dart - * library that contains the app's main() function. If this is nil, it will - * default to `main()`. If it is not the app's main() function, that function - * must be decorated with `@pragma(vm:entry-point)` to ensure the method is not - * tree-shaken by the Dart compiler. + * library that contains the app's main() function. If this is FlutterDefaultDartEntrypoint (or + * nil) it will default to `main()`. If it is not the app's main() function, that function must + * be decorated with `@pragma(vm:entry-point)` to ensure the method is not tree-shaken by the Dart + * compiler. * @return YES if the call succeeds in creating and running a Flutter Engine instance; NO otherwise. */ - (BOOL)runWithEntrypoint:(nullable NSString*)entrypoint; @@ -114,10 +151,10 @@ FLUTTER_EXPORT * The first call to this method will create a new Isolate. Subsequent calls will return * immediately. * - * @param entrypoint The name of a top-level function from a Dart library. If nil, this will - * default to `main()`. If it is not the app's main() function, that function - * must be decorated with `@pragma(vm:entry-point)` to ensure the method is not - * tree-shaken by the Dart compiler. + * @param entrypoint The name of a top-level function from a Dart library. If this is + * FlutterDefaultDartEntrypoint (or nil); this will default to `main()`. If it is not the app's + * main() function, that function must be decorated with `@pragma(vm:entry-point)` to ensure the + * method is not tree-shaken by the Dart compiler. * @param uri The URI of the Dart library which contains the entrypoint method. IF nil, * this will default to the same library as the `main()` function in the Dart program. * @return YES if the call succeeds in creating and running a Flutter Engine instance; NO otherwise. diff --git a/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm b/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm index b2374fa0d147c..2cfbbdb4849dc 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm @@ -27,6 +27,8 @@ #import "flutter/shell/platform/darwin/ios/ios_surface.h" #import "flutter/shell/platform/darwin/ios/platform_view_ios.h" +NSString* const FlutterDefaultDartEntrypoint = nil; + @interface FlutterEngine () // Maintains a dictionary of plugin names that have registered with the engine. Used by // FlutterEngineRegistrar to implement a FlutterPluginRegistrar. @@ -70,6 +72,10 @@ @implementation FlutterEngine { FlutterBinaryMessengerRelay* _binaryMessenger; } +- (instancetype)initWithName:(NSString*)labelPrefix { + return [self initWithName:labelPrefix project:nil allowHeadlessExecution:YES]; +} + - (instancetype)initWithName:(NSString*)labelPrefix project:(FlutterDartProject*)project { return [self initWithName:labelPrefix project:project allowHeadlessExecution:YES]; } @@ -429,6 +435,10 @@ - (BOOL)createShell:(NSString*)entrypoint libraryURI:(NSString*)libraryURI { return _shell != nullptr; } +- (BOOL)run { + return [self runWithEntrypoint:FlutterDefaultDartEntrypoint libraryURI:nil]; +} + - (BOOL)runWithEntrypoint:(NSString*)entrypoint libraryURI:(NSString*)libraryURI { if ([self createShell:entrypoint libraryURI:libraryURI]) { [self launchEngine:entrypoint libraryURI:libraryURI];