From dcf2892b233ac86d44100b2bd506b961246288ec Mon Sep 17 00:00:00 2001 From: John Ryan Date: Fri, 24 Apr 2026 09:26:53 -0700 Subject: [PATCH] Update iOS snippets to reflect UISceneDelegate adoption - Update platform-channels.md to use didInitializeImplicitFlutterEngine. - Update platform-views.md to use didInitializeImplicitFlutterEngine and add protocol conformance. - Update add-flutter-screen.md to include configurationForConnecting in AppDelegate. Fixes https://github.com/flutter/website/issues/13087 Fixes https://github.com/flutter/website/issues/13086 Fixes https://github.com/flutter/website/issues/13274 --- .../add-to-app/ios/add-flutter-screen.md | 41 +++++++++++++++++++ .../ios/platform-views.md | 7 +++- .../platform-integration/platform-channels.md | 9 ++-- 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/src/content/add-to-app/ios/add-flutter-screen.md b/src/content/add-to-app/ios/add-flutter-screen.md index 94bcdd92621..d4f450edeb1 100644 --- a/src/content/add-to-app/ios/add-flutter-screen.md +++ b/src/content/add-to-app/ios/add-flutter-screen.md @@ -101,6 +101,19 @@ class AppDelegate: FlutterAppDelegate { // More on the FlutterAppDelegate. GeneratedPluginRegistrant.register(with: self.flutterEngine); return super.application(application, didFinishLaunchingWithOptions: launchOptions); } + + override func application( + _ application: UIApplication, + configurationForConnecting connectingSceneSession: UISceneSession, + options: UIScene.ConnectionOptions + ) -> UISceneConfiguration { + let configuration = UISceneConfiguration( + name: nil, + sessionRole: connectingSceneSession.role + ) + configuration.delegateClass = FlutterSceneDelegate.self + return configuration + } } ``` @@ -137,6 +150,15 @@ exposed as a property, on app startup in the app delegate. return [super application:application didFinishLaunchingWithOptions:launchOptions]; } +- (UISceneConfiguration *)application:(UIApplication *)application + configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession + options:(UISceneConnectionOptions *)options { + UISceneConfiguration *configuration = [[UISceneConfiguration alloc] initWithName:nil + sessionRole:connectingSceneSession.role]; + configuration.delegateClass = [FlutterSceneDelegate class]; + return configuration; +} + @end ``` @@ -375,6 +397,11 @@ The `FlutterAppDelegate` performs functions such as: * Keeping the Flutter connection open in debug mode when the phone screen locks. +As of Flutter 3.41, `UIScene` support is the default for iOS apps. +When using `FlutterAppDelegate`, you should also ensure that your app +uses `FlutterSceneDelegate` (or a subclass) to receive scene lifecycle +events, such as [`openURL`][] and [`continueUserActivity`][]. + ### Creating a FlutterAppDelegate subclass Creating a subclass of the `FlutterAppDelegate` in UIKit apps was shown in the [Start a FlutterEngine and FlutterViewController section][]. @@ -399,6 +426,19 @@ class AppDelegate: FlutterAppDelegate { GeneratedPluginRegistrant.register(with: self.flutterEngine); return true; } + + override func application( + _ application: UIApplication, + configurationForConnecting connectingSceneSession: UISceneSession, + options: UIScene.ConnectionOptions + ) -> UISceneConfiguration { + let configuration = UISceneConfiguration( + name: nil, + sessionRole: connectingSceneSession.role + ) + configuration.delegateClass = FlutterSceneDelegate.self + return configuration + } } @main @@ -841,6 +881,7 @@ For a working example, refer to this [sample project][]. [`runApp`]: {{site.api}}/flutter/widgets/runApp.html [`runWithEntrypoint`]: {{site.api}}/ios-embedder/interface_flutter_engine.html#a019d6b3037eff6cfd584fb2eb8e9035e [`SystemNavigator.pop()`]: {{site.api}}/flutter/services/SystemNavigator/pop.html +[`continueUserActivity`]: {{site.apple-dev}}/documentation/uikit/uiapplicationdelegate/1623072-application [tree-shaken]: https://en.wikipedia.org/wiki/Tree_shaking [`WidgetsApp`]: {{site.api}}/flutter/widgets/WidgetsApp-class.html [`PlatformDispatcher.defaultRouteName`]: {{site.api}}/flutter/dart-ui/PlatformDispatcher/defaultRouteName.html diff --git a/src/content/platform-integration/ios/platform-views.md b/src/content/platform-integration/ios/platform-views.md index 9c4d88181ef..94134231302 100644 --- a/src/content/platform-integration/ios/platform-views.md +++ b/src/content/platform-integration/ios/platform-views.md @@ -163,7 +163,7 @@ Finally, register the platform view. This can be done in an app or a plugin. For app registration, -modify the App's `AppDelegate.swift`: +implement the `didInitializeImplicitFlutterEngine:` method in the App's `AppDelegate.swift`: ```swift import Flutter @@ -286,13 +286,16 @@ Finally, register the platform view. This can be done in an app or a plugin. For app registration, -modify the App's `AppDelegate.m`: +implement the `didInitializeImplicitFlutterEngine:` method in the App's `AppDelegate.m`: ```objc #import "AppDelegate.h" #import "FLNativeView.h" #import "GeneratedPluginRegistrant.h" +@interface AppDelegate () +@end + @implementation AppDelegate - (void)didInitializeImplicitFlutterEngine:(NSObject*)engineBridge { diff --git a/src/content/platform-integration/platform-channels.md b/src/content/platform-integration/platform-channels.md index 61f6c86b50c..d78eae54a84 100644 --- a/src/content/platform-integration/platform-channels.md +++ b/src/content/platform-integration/platform-channels.md @@ -608,7 +608,7 @@ Add support for Swift in the standard template setup that uses Objective-C: 1. Open the file `AppDelegate.swift` located under **Runner > Runner** in the Project navigator. -Override the `application:didFinishLaunchingWithOptions:` function and create +Implement the `didInitializeImplicitFlutterEngine:` method and create a `FlutterMethodChannel` tied to the channel name `samples.flutter.dev/battery`: @@ -696,8 +696,8 @@ Start by opening the iOS host portion of the Flutter app in Xcode: 1. Open the file `AppDelegate.m`, located under **Runner > Runner** in the Project navigator. -Create a `FlutterMethodChannel` and add a handler inside the `application -didFinishLaunchingWithOptions:` method. +Create a `FlutterMethodChannel` and add a handler inside the +`didInitializeImplicitFlutterEngine:` method. Make sure to use the same channel name as was used on the Flutter client side. @@ -712,6 +712,9 @@ create your `FlutterMethodChannel` in the `didInitializeImplicitFlutterEngine` m #import #import "GeneratedPluginRegistrant.h" +@interface AppDelegate () +@end + @implementation AppDelegate - (void)didInitializeImplicitFlutterEngine:(NSObject*)engineBridge {