text
stringlengths
1
372
// 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) {
lifecycleDelegate.application(application, handleEventsForBackgroundURLSession: identifier, completionHandler: completionHandler)
}
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (uibackgroundfetchresult) -> void) {
lifecycleDelegate.application(application, performFetchWithCompletionHandler: completionHandler)
}
func add(_ delegate: FlutterApplicationLifeCycleDelegate) {
lifecycleDelegate.add(delegate)
}
}
<code_end>
<code_start>
@import flutter;
@import UIKit;
@import FlutterPluginRegistrant;
@interface AppDelegate : UIResponder <uiapplicationdelegate, FlutterAppLifeCycleProvider>
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic,strong) FlutterEngine *flutterengine;
@end
<code_end>
the implementation should delegate mostly to a
FlutterPluginAppLifeCycleDelegate:
<code_start>
@interface AppDelegate ()
@property (nonatomic, strong) FlutterPluginAppLifeCycleDelegate* lifeCycleDelegate;
@end
@implementation AppDelegate
- (instancetype)init {
if (self = [super init]) {
_lifeCycleDelegate = [[flutterpluginapplifecycledelegate alloc] init];
}
return self;
}
- (bool)application:(uiapplication*)application
didFinishLaunchingWithOptions:(NSDictionary<UIApplicationLaunchOptionsKey, id>*))launchOptions {
self.flutterEngine = [[flutterengine alloc] initWithName:@"io.flutter" project:nil];