FlutterViewController.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Copyright 2013 The Flutter Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef FLUTTER_FLUTTERVIEWCONTROLLER_H_
  5. #define FLUTTER_FLUTTERVIEWCONTROLLER_H_
  6. #import <UIKit/UIKit.h>
  7. #include <sys/cdefs.h>
  8. #include "FlutterBinaryMessenger.h"
  9. #include "FlutterDartProject.h"
  10. #include "FlutterEngine.h"
  11. #include "FlutterMacros.h"
  12. #include "FlutterPlugin.h"
  13. #include "FlutterTexture.h"
  14. NS_ASSUME_NONNULL_BEGIN
  15. @class FlutterEngine;
  16. /**
  17. * The name used for semantic update notifications via `NSNotificationCenter`.
  18. *
  19. * The object passed as the sender is the `FlutterViewController` associated
  20. * with the update.
  21. */
  22. FLUTTER_EXPORT
  23. extern NSNotificationName const FlutterSemanticsUpdateNotification;
  24. /**
  25. * A `UIViewController` implementation for Flutter views.
  26. *
  27. * Dart execution, channel communication, texture registration, and plugin registration are all
  28. * handled by `FlutterEngine`. Calls on this class to those members all proxy through to the
  29. * `FlutterEngine` attached FlutterViewController.
  30. *
  31. * A FlutterViewController can be initialized either with an already-running `FlutterEngine` via the
  32. * `initWithEngine:` initializer, or it can be initialized with a `FlutterDartProject` that will be
  33. * used to implicitly spin up a new `FlutterEngine`. Creating a `FlutterEngine` before showing a
  34. * FlutterViewController can be used to pre-initialize the Dart VM and to prepare the isolate in
  35. * order to reduce the latency to the first rendered frame. See
  36. * https://flutter.dev/docs/development/add-to-app/performance for more details on loading
  37. * latency.
  38. *
  39. * Holding a `FlutterEngine` independently of FlutterViewControllers can also be used to not to lose
  40. * Dart-related state and asynchronous tasks when navigating back and forth between a
  41. * FlutterViewController and other `UIViewController`s.
  42. */
  43. FLUTTER_EXPORT
  44. @interface FlutterViewController : UIViewController <FlutterTextureRegistry, FlutterPluginRegistry>
  45. /**
  46. * Initializes this FlutterViewController with the specified `FlutterEngine`.
  47. *
  48. * The initialized viewcontroller will attach itself to the engine as part of this process.
  49. *
  50. * @param engine The `FlutterEngine` instance to attach to.
  51. * @param nibName The NIB name to initialize this UIViewController with.
  52. * @param nibBundle The NIB bundle.
  53. */
  54. - (instancetype)initWithEngine:(FlutterEngine*)engine
  55. nibName:(nullable NSString*)nibName
  56. bundle:(nullable NSBundle*)nibBundle NS_DESIGNATED_INITIALIZER;
  57. /**
  58. * Initializes a new FlutterViewController and `FlutterEngine` with the specified
  59. * `FlutterDartProject`.
  60. *
  61. * This will implicitly create a new `FlutterEngine` which is retrievable via the `engine` property
  62. * after initialization.
  63. *
  64. * @param project The `FlutterDartProject` to initialize the `FlutterEngine` with.
  65. * @param nibName The NIB name to initialize this UIViewController with.
  66. * @param nibBundle The NIB bundle.
  67. */
  68. - (instancetype)initWithProject:(nullable FlutterDartProject*)project
  69. nibName:(nullable NSString*)nibName
  70. bundle:(nullable NSBundle*)nibBundle NS_DESIGNATED_INITIALIZER;
  71. /**
  72. * Initializer that is called from loading a FlutterViewController from a XIB.
  73. *
  74. * See also:
  75. * https://developer.apple.com/documentation/foundation/nscoding/1416145-initwithcoder?language=objc
  76. */
  77. - (instancetype)initWithCoder:(NSCoder*)aDecoder NS_DESIGNATED_INITIALIZER;
  78. /**
  79. * Registers a callback that will be invoked when the Flutter view has been rendered.
  80. * The callback will be fired only once.
  81. *
  82. * Replaces an existing callback. Use a `nil` callback to unregister the existing one.
  83. */
  84. - (void)setFlutterViewDidRenderCallback:(void (^)(void))callback;
  85. /**
  86. * Returns the file name for the given asset.
  87. * The returned file name can be used to access the asset in the application's
  88. * main bundle.
  89. *
  90. * @param asset The name of the asset. The name can be hierarchical.
  91. * @return The file name to be used for lookup in the main bundle.
  92. */
  93. - (NSString*)lookupKeyForAsset:(NSString*)asset;
  94. /**
  95. * Returns the file name for the given asset which originates from the specified
  96. * package.
  97. * The returned file name can be used to access the asset in the application's
  98. * main bundle.
  99. *
  100. * @param asset The name of the asset. The name can be hierarchical.
  101. * @param package The name of the package from which the asset originates.
  102. * @return The file name to be used for lookup in the main bundle.
  103. */
  104. - (NSString*)lookupKeyForAsset:(NSString*)asset fromPackage:(NSString*)package;
  105. /**
  106. * Attempts to set the first route that the Flutter app shows if the Flutter
  107. * runtime hasn't yet started. The default is "/".
  108. *
  109. * This method must be called immediately after `initWithProject` and has no
  110. * effect when using `initWithEngine` if the `FlutterEngine` has already been
  111. * run.
  112. *
  113. * Setting this after the Flutter started running has no effect. See `pushRoute`
  114. * and `popRoute` to change the route after Flutter started running.
  115. *
  116. * @param route The name of the first route to show.
  117. */
  118. - (void)setInitialRoute:(NSString*)route;
  119. /**
  120. * Instructs the Flutter Navigator (if any) to go back.
  121. */
  122. - (void)popRoute;
  123. /**
  124. * Instructs the Flutter Navigator (if any) to push a route on to the navigation
  125. * stack. The setInitialRoute method should be preferred if this is called before the
  126. * FlutterViewController has come into view.
  127. *
  128. * @param route The name of the route to push to the navigation stack.
  129. */
  130. - (void)pushRoute:(NSString*)route;
  131. /**
  132. * The `FlutterPluginRegistry` used by this FlutterViewController.
  133. */
  134. - (id<FlutterPluginRegistry>)pluginRegistry;
  135. /**
  136. * True if at least one frame has rendered and the ViewController has appeared.
  137. *
  138. * This property is reset to false when the ViewController disappears. It is
  139. * guaranteed to only alternate between true and false for observers.
  140. */
  141. @property(nonatomic, readonly, getter=isDisplayingFlutterUI) BOOL displayingFlutterUI;
  142. /**
  143. * Specifies the view to use as a splash screen. Flutter's rendering is asynchronous, so the first
  144. * frame rendered by the Flutter application might not immediately appear when theFlutter view is
  145. * initially placed in the view hierarchy. The splash screen view will be used as
  146. * a replacement until the first frame is rendered.
  147. *
  148. * The view used should be appropriate for multiple sizes; an autoresizing mask to
  149. * have a flexible width and height will be applied automatically.
  150. */
  151. @property(strong, nonatomic) UIView* splashScreenView;
  152. /**
  153. * Attempts to set the `splashScreenView` property from the `UILaunchStoryboardName` from the
  154. * main bundle's `Info.plist` file. This method will not change the value of `splashScreenView`
  155. * if it cannot find a default one from a storyboard or nib.
  156. *
  157. * @return `YES` if successful, `NO` otherwise.
  158. */
  159. - (BOOL)loadDefaultSplashScreenView;
  160. /**
  161. * Controls whether the created view will be opaque or not.
  162. *
  163. * Default is `YES`. Note that setting this to `NO` may negatively impact performance
  164. * when using hardware acceleration, and toggling this will trigger a re-layout of the
  165. * view.
  166. */
  167. @property(nonatomic, getter=isViewOpaque) BOOL viewOpaque;
  168. /**
  169. * The `FlutterEngine` instance for this view controller. This could be the engine this
  170. * `FlutterViewController` is initialized with or a new `FlutterEngine` implicitly created if
  171. * no engine was supplied during initialization.
  172. */
  173. @property(weak, nonatomic, readonly) FlutterEngine* engine;
  174. /**
  175. * The `FlutterBinaryMessenger` associated with this FlutterViewController (used for communicating
  176. * with channels).
  177. *
  178. * This is just a convenient way to get the |FlutterEngine|'s binary messenger.
  179. */
  180. @property(nonatomic, readonly) NSObject<FlutterBinaryMessenger>* binaryMessenger;
  181. /**
  182. * If the `FlutterViewController` creates a `FlutterEngine`, this property
  183. * determines if that `FlutterEngine` has `allowHeadlessExecution` set.
  184. *
  185. * The intention is that this is used with the XIB. Otherwise, a
  186. * `FlutterEngine` can just be sent to the init methods.
  187. *
  188. * See also: `-[FlutterEngine initWithName:project:allowHeadlessExecution:]`
  189. */
  190. @property(nonatomic, readonly) BOOL engineAllowHeadlessExecution;
  191. @end
  192. NS_ASSUME_NONNULL_END
  193. #endif // FLUTTER_FLUTTERVIEWCONTROLLER_H_