text stringlengths 1 474 |
|---|
- (void)viewDidLoad { |
[super viewDidLoad]; |
// Make a button to call the showFlutter function when pressed. |
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; |
[button addTarget:self |
action:@selector(showFlutter) |
forControlEvents:UIControlEventTouchUpInside]; |
[button setTitle:@"Show Flutter!" forState:UIControlStateNormal]; |
button.backgroundColor = UIColor.blueColor; |
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0); |
[self.view addSubview:button]; |
} |
- (void)showFlutter { |
FlutterEngine *flutterEngine = |
((AppDelegate *)UIApplication.sharedApplication.delegate).flutterEngine; |
FlutterViewController *flutterViewController = |
[[FlutterViewController alloc] initWithEngine:flutterEngine nibName:nil bundle:nil]; |
[self presentViewController:flutterViewController animated:YES completion:nil]; |
} |
@end<code_end> |
Now, you have a Flutter screen embedded in your iOS app.info Note |
Using the previous example, the default main() |
entrypoint function of your default Dart library |
would run when calling run on the |
FlutterEngine created in the AppDelegate.<topic_end> |
<topic_start> |
Alternatively - Create a FlutterViewController with an implicit FlutterEngine |
As an alternative to the previous example, you can let the |
FlutterViewController implicitly create its own FlutterEngine without |
pre-warming one ahead of time.This is not usually recommended because creating a |
FlutterEngine on-demand could introduce a noticeable |
latency between when the FlutterViewController is |
presented and when it renders its first frame. This could, however, be |
useful if the Flutter screen is rarely shown, when there are no good |
heuristics to determine when the Dart VM should be started, and when Flutter |
doesn’t need to persist state between view controllers.To let the FlutterViewController present without an existing |
FlutterEngine, omit the FlutterEngine construction, and create the |
FlutterViewController without an engine reference. |
<code_start>// Existing code omitted. |
func showFlutter() { |
let flutterViewController = FlutterViewController(project: nil, nibName: nil, bundle: nil) |
present(flutterViewController, animated: true, completion: nil) |
}<code_end> |
<code_start>// Existing code omitted. |
- (void)showFlutter { |
FlutterViewController *flutterViewController = |
[[FlutterViewController alloc] initWithProject:nil nibName:nil bundle:nil]; |
[self presentViewController:flutterViewController animated:YES completion:nil]; |
} |
@end<code_end> |
See Loading sequence and performance |
for more explorations on latency and memory usage.<topic_end> |
<topic_start> |
Using the FlutterAppDelegate |
Letting your application’s UIApplicationDelegate subclass |
FlutterAppDelegate is recommended but not required.The FlutterAppDelegate performs functions such as:<topic_end> |
<topic_start> |
Creating a FlutterAppDelegate subclass |
Creating a subclass of the FlutterAppDelegate in UIKit apps was shown |
in the Start a FlutterEngine and FlutterViewController section. |
In a SwiftUI app, you can create a subclass of the |
FlutterAppDelegate that conforms to the ObservableObject protocol as follows:Then, in your view, the AppDelegateis accessible as an EnvironmentObject.<topic_end> |
<topic_start> |
If you can’t directly make FlutterAppDelegate a subclass |
If your app delegate can’t directly make FlutterAppDelegate a subclass, |
make your app delegate implement the FlutterAppLifeCycleProvider |
protocol in order to make sure your plugins receive the necessary callbacks. |
Otherwise, plugins that depend on these events might have undefined behavior.For instance: |
<code_start>import Foundation |
import Flutter |
class AppDelegate: UIResponder, UIApplicationDelegate, FlutterAppLifeCycleProvider, ObservableObject { |
private let lifecycleDelegate = FlutterPluginAppLifeCycleDelegate() |
let flutterEngine = FlutterEngine(name: "flutter_nps_engine") |
override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { |
flutterEngine.run() |
return lifecycleDelegate.application(application, didFinishLaunchingWithOptions: launchOptions ?? [:]) |
} |
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { |
lifecycleDelegate.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken) |
} |
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { |
lifecycleDelegate.application(application, didFailToRegisterForRemoteNotificationsWithError: error) |
} |
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { |
lifecycleDelegate.application(application, didReceiveRemoteNotification: userInfo, fetchCompletionHandler: completionHandler) |
} |
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { |
return lifecycleDelegate.application(app, open: url, options: options) |
} |
func application(_ application: UIApplication, handleOpen url: URL) -> Bool { |
return lifecycleDelegate.application(application, handleOpen: url) |
} |
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool { |
return lifecycleDelegate.application(application, open: url, sourceApplication: sourceApplication ?? "", annotation: annotation) |
} |
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) { |
lifecycleDelegate.application(application, performActionFor: shortcutItem, completionHandler: completionHandler) |
} |
func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) { |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.