1
0
mirror of https://github.com/flutter/samples.git synced 2026-10-09 18:33:18 +00:00

Migrate standard sample apps to UIScene lifecycle (#2884)

Fixes https://github.com/flutter/flutter/issues/176957
This commit is contained in:
LouiseHsu
2026-09-30 11:27:28 -07:00
committed by GitHub
parent 8a4cf1db16
commit a05867daaa
55 changed files with 811 additions and 268 deletions

View File

@@ -2,88 +2,111 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import UIKit
import Flutter
import UIKit
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
let flutterViewController = window.rootViewController as! FlutterViewController
FlutterMethodChannel(name: "methodChannelDemo", binaryMessenger: flutterViewController.binaryMessenger).setMethodCallHandler({
(call: FlutterMethodCall, result: FlutterResult) -> Void in
guard let count = (call.arguments as? NSDictionary)?["count"] as? Int else {
result(FlutterError(code: "INVALID_ARGUMENT", message: "Value of count cannot be null", details: nil))
return
}
switch call.method {
case "increment":
result(count + 1)
case "decrement":
result(count - 1)
default:
result(FlutterMethodNotImplemented)
}
})
FlutterBasicMessageChannel(name: "platformImageDemo", binaryMessenger: flutterViewController.binaryMessenger, codec: FlutterStandardMessageCodec.sharedInstance()).setMessageHandler{
(message: Any?, reply: FlutterReply) -> Void in
if(message as! String == "getImage") {
guard let image = UIImage(named: "eat_new_orleans.jpg") else {
reply(nil)
return
}
reply(FlutterStandardTypedData(bytes: image.jpegData(compressionQuality: 1)!))
}
@main
@objc class AppDelegate: FlutterAppDelegate, FlutterImplicitEngineDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
func didInitializeImplicitFlutterEngine(_ engineBridge: FlutterImplicitEngineBridge) {
GeneratedPluginRegistrant.register(with: engineBridge.pluginRegistry)
let messenger = engineBridge.applicationRegistrar.messenger()
FlutterMethodChannel(name: "methodChannelDemo", binaryMessenger: messenger)
.setMethodCallHandler({ (call: FlutterMethodCall, result: FlutterResult) -> Void in
guard let count = (call.arguments as? NSDictionary)?["count"] as? Int else {
result(
FlutterError(
code: "INVALID_ARGUMENT",
message: "Value of count cannot be null",
details: nil
)
)
return
}
FlutterEventChannel(name: "eventChannelDemo", binaryMessenger: flutterViewController.binaryMessenger).setStreamHandler(AccelerometerStreamHandler())
var petList : [[String:String]] = []
// A FlutterBasicMessageChannel for sending petList to Dart.
let stringCodecChannel = FlutterBasicMessageChannel(name: "stringCodecDemo", binaryMessenger: flutterViewController.binaryMessenger, codec: FlutterStringCodec.sharedInstance())
// Registers a MessageHandler for FlutterBasicMessageChannel to receive pet details.
FlutterBasicMessageChannel(name: "jsonMessageCodecDemo", binaryMessenger: flutterViewController.binaryMessenger, codec: FlutterJSONMessageCodec.sharedInstance())
.setMessageHandler{(message: Any?, reply: FlutterReply) -> Void in
petList.insert(message! as! [String: String], at: 0)
stringCodecChannel.sendMessage(self.convertPetListToJson(petList: petList))
}
// Registers a MessageHandler for FlutterBasicMessageHandler to receive indices of detail records to remove from the petList.
FlutterBasicMessageChannel(name: "binaryCodecDemo", binaryMessenger: flutterViewController.binaryMessenger, codec: FlutterBinaryCodec.sharedInstance()).setMessageHandler{
(message: Any?, reply: FlutterReply) -> Void in
guard let index = Int.init(String.init(data: message! as! Data, encoding: String.Encoding.utf8)!) else {
reply(nil)
return
}
if (index >= 0 && index < petList.count) {
petList.remove(at: index)
reply("Removed Successfully".data(using: .utf8)!)
stringCodecChannel.sendMessage(self.convertPetListToJson(petList: petList))
} else {
reply(nil)
}
switch call.method {
case "increment":
result(count + 1)
case "decrement":
result(count - 1)
default:
result(FlutterMethodNotImplemented)
}
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
})
FlutterBasicMessageChannel(
name: "platformImageDemo",
binaryMessenger: messenger,
codec: FlutterStandardMessageCodec.sharedInstance()
).setMessageHandler { (message: Any?, reply: FlutterReply) -> Void in
if message as! String == "getImage" {
guard let image = UIImage(named: "eat_new_orleans.jpg") else {
reply(nil)
return
}
reply(FlutterStandardTypedData(bytes: image.jpegData(compressionQuality: 1)!))
}
}
// Function to convert petList to json string.
func convertPetListToJson(petList: [[String: String]]) -> String? {
guard let data = try? JSONSerialization.data(withJSONObject: ["petList": petList], options: .prettyPrinted) else {
return nil
}
return String(data: data, encoding: String.Encoding.utf8)
FlutterEventChannel(
name: "eventChannelDemo",
binaryMessenger: messenger
).setStreamHandler(AccelerometerStreamHandler())
var petList: [[String: String]] = []
// A FlutterBasicMessageChannel for sending petList to Dart.
let stringCodecChannel = FlutterBasicMessageChannel(
name: "stringCodecDemo",
binaryMessenger: messenger,
codec: FlutterStringCodec.sharedInstance()
)
// Registers a MessageHandler for FlutterBasicMessageChannel to receive pet details.
FlutterBasicMessageChannel(
name: "jsonMessageCodecDemo",
binaryMessenger: messenger,
codec: FlutterJSONMessageCodec.sharedInstance()
).setMessageHandler { (message: Any?, reply: FlutterReply) -> Void in
petList.insert(message! as! [String: String], at: 0)
stringCodecChannel.sendMessage(self.convertPetListToJson(petList: petList))
}
// Registers a MessageHandler for FlutterBasicMessageHandler to receive indices of detail records to remove from the petList.
FlutterBasicMessageChannel(
name: "binaryCodecDemo",
binaryMessenger: messenger,
codec: FlutterBinaryCodec.sharedInstance()
).setMessageHandler { (message: Any?, reply: FlutterReply) -> Void in
guard let index = Int(String(data: message! as! Data, encoding: String.Encoding.utf8)!) else {
reply(nil)
return
}
if index >= 0 && index < petList.count {
petList.remove(at: index)
reply("Removed Successfully".data(using: .utf8)!)
stringCodecChannel.sendMessage(self.convertPetListToJson(petList: petList))
} else {
reply(nil)
}
}
}
// Function to convert petList to json string.
func convertPetListToJson(petList: [[String: String]]) -> String? {
guard let data = try? JSONSerialization.data(withJSONObject: ["petList": petList], options: .prettyPrinted) else {
return nil
}
return String(data: data, encoding: String.Encoding.utf8)
}
}

View File

@@ -24,6 +24,27 @@
<string>$(FLUTTER_BUILD_NUMBER)</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
<false/>
<key>UISceneConfigurations</key>
<dict>
<key>UIWindowSceneSessionRoleApplication</key>
<array>
<dict>
<key>UISceneClassName</key>
<string>UIWindowScene</string>
<key>UISceneConfigurationName</key>
<string>flutter</string>
<key>UISceneDelegateClassName</key>
<string>FlutterSceneDelegate</string>
<key>UISceneStoryboardFile</key>
<string>Main</string>
</dict>
</array>
</dict>
</dict>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>