Migrate ios_books SceneDelegate for UIScene (#2845)

Part of flutter/flutter#176957

Migrated SceneDelegate.swift to use UI scene using this guideline
https://docs.flutter.dev/release/breaking-changes/uiscenedelegate#migration-guide-for-flutter-apps
for ios_books app

<img width="1931" height="1080" alt="image"
src="https://github.com/user-attachments/assets/425f7cee-205b-4cf5-82fa-d95ff8227c1e"
/>

While testing this sample locally, I noticed a separate existing issue
in `IosBooks/ViewController.swift`: the Google Books response is
force-unwrapped. The unauthenticated request currently returns HTTP
429/quota errors in some environments, so `json["items"] as! [[String:
Any]]` can crash at runtime.

I kept this PR scoped to the UIScene migration only, but I can file or
send a separate follow-up to make the sample parse the API response
defensively, WDYT?


## Pre-launch Checklist

- [x] I read the [Flutter Style Guide] _recently_, and have followed its
advice.
- [x] I signed the [CLA].
- [x] I read the [Contributors Guide].
- [x] I have added sample code updates to the [changelog].
- [x] I updated/added relevant documentation (doc comments with `///`).


If you need help, consider asking for advice on the #hackers-devrel
channel on [Discord].

<!-- Links -->
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/master/docs/contributing/Style-guide-for-Flutter-repo.md
[CLA]: https://cla.developers.google.com/
[Discord]:
https://github.com/flutter/flutter/blob/master/docs/contributing/Chat.md
[Contributors Guide]:
https://github.com/flutter/samples/blob/main/CONTRIBUTING.md
[changelog]: ../CHANGELOG.md

Co-authored-by: Eric Windmill <eric@ericwindmill.com>
main
Aleksandr Lozhkovoi 2 days ago committed by GitHub
parent 7e12f75def
commit a6754b4515
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -2,31 +2,68 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import Flutter
import UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
class SceneDelegate: UIResponder, UIWindowSceneDelegate, FlutterSceneLifeCycleProvider
{
var sceneLifeCycleDelegate: FlutterPluginSceneLifeCycleDelegate =
FlutterPluginSceneLifeCycleDelegate()
var window: UIWindow?
func scene(
_ scene: UIScene, willConnectTo session: UISceneSession,
_ scene: UIScene,
willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions
) {
guard let _ = (scene as? UIWindowScene) else { return }
sceneLifeCycleDelegate.scene(
scene,
willConnectTo: session,
options: connectionOptions
)
}
func sceneDidDisconnect(_ scene: UIScene) {
sceneLifeCycleDelegate.sceneDidDisconnect(scene)
}
func sceneWillEnterForeground(_ scene: UIScene) {
sceneLifeCycleDelegate.sceneWillEnterForeground(scene)
}
func sceneDidBecomeActive(_ scene: UIScene) {
sceneLifeCycleDelegate.sceneDidBecomeActive(scene)
}
func sceneWillResignActive(_ scene: UIScene) {
sceneLifeCycleDelegate.sceneWillResignActive(scene)
}
func sceneWillEnterForeground(_ scene: UIScene) {
func sceneDidEnterBackground(_ scene: UIScene) {
sceneLifeCycleDelegate.sceneDidEnterBackground(scene)
}
func sceneDidEnterBackground(_ scene: UIScene) {
func scene(
_ scene: UIScene,
openURLContexts URLContexts: Set<UIOpenURLContext>
) {
sceneLifeCycleDelegate.scene(scene, openURLContexts: URLContexts)
}
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
sceneLifeCycleDelegate.scene(scene, continue: userActivity)
}
func windowScene(
_ windowScene: UIWindowScene,
performActionFor shortcutItem: UIApplicationShortcutItem,
completionHandler: @escaping (Bool) -> Void
) {
sceneLifeCycleDelegate.windowScene(
windowScene,
performActionFor: shortcutItem,
completionHandler: completionHandler
)
}
}

Loading…
Cancel
Save