text
stringlengths
1
372
in this example, we create a FlutterEngine object inside a SwiftUI ObservableObject.
we then pass this FlutterEngine into a ContentView using the
environmentObject() property.
<code_start>
import SwiftUI
import flutter
// the following library connects plugins with iOS platform code to this app.
import FlutterPluginRegistrant
class FlutterDependencies: ObservableObject {
let flutterEngine = FlutterEngine(name: "my flutter engine")
init(){
// runs the default dart entrypoint with a default flutter route.
flutterEngine.run()
// connects plugins with iOS platform code to this app.
GeneratedPluginRegistrant.register(with: self.flutterEngine);
}
}
@main
struct MyApp: app {
// flutterDependencies will be injected using EnvironmentObject.
@stateobject var flutterDependencies = FlutterDependencies()
var body: some scene {
WindowGroup {
ContentView().environmentObject(flutterDependencies)
}
}
}
<code_end>
as an example, we demonstrate creating a
FlutterEngine, exposed as a property, on app startup in
the app delegate.
<code_start>
import UIKit
import flutter
// the following library connects plugins with iOS platform code to this app.
import FlutterPluginRegistrant
@uiapplicationmain
class AppDelegate: FlutterAppDelegate { // more on the FlutterAppDelegate.
lazy var flutterEngine = FlutterEngine(name: "my flutter engine")
override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [uiapplication.launchoptionskey: any]?) -> bool {
// runs the default dart entrypoint with a default flutter route.
flutterEngine.run();
// connects plugins with iOS platform code to this app.
GeneratedPluginRegistrant.register(with: self.flutterEngine);
return super.application(application, didFinishLaunchingWithOptions: launchOptions);
}
}
<code_end>
in this example, we create a FlutterEngine
object inside a SwiftUI ObservableObject.
we then pass this FlutterEngine into a
ContentView using the environmentObject() property.
<code_start>
@import UIKit;
@import flutter;
@interface AppDelegate : FlutterAppDelegate // more on the FlutterAppDelegate below.
@property (nonatomic,strong) FlutterEngine *flutterengine;
@end
<code_end>
<code_start>
// the following library connects plugins with iOS platform code to this app.
#import <flutterpluginregistrant/generatedpluginregistrant.h>
#import "appdelegate.h"
@implementation AppDelegate
- (bool)application:(uiapplication *)application
didFinishLaunchingWithOptions:(NSDictionary<UIApplicationLaunchOptionsKey, id> *)launchoptions {
self.flutterEngine = [[flutterengine alloc] initWithName:@"my flutter engine"];
// runs the default dart entrypoint with a default flutter route.
[self.flutterengine run];
// connects plugins with iOS platform code to this app.
[generatedpluginregistrant registerWithRegistry:self.flutterEngine];
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
@end
<code_end>
<topic_end>
<topic_start>
show a FlutterViewController with your FlutterEngine
the following example shows a generic ContentView with a
button hooked to present a FlutterViewController.
the FlutterViewController constructor takes the pre-warmed
FlutterEngine as an argument. FlutterEngine is passed in
as an EnvironmentObject via flutterDependencies.
<code_start>
import SwiftUI
import flutter
struct ContentView: view {
// flutter dependencies are passed in an EnvironmentObject.
@environmentobject var flutterDependencies: FlutterDependencies
// button is created to call the showFlutter function when pressed.
var body: some view {
Button("Show flutter!") {
showFlutter()
}
}
func showFlutter() {
// get the RootViewController.
guard
let windowScene = UIApplication.shared.connectedScenes
.first(where: { $0.activationstate == .foregroundactive && $0 is UIWindowScene }) as? UIWindowScene,