草庐IT

[Swift] AppDelegate 与 SceneDelegate

巨馍蘸酱 2023-03-28 原文

先说结论:
SceneDelegate 用于 iPad 分屏 APP 开发, 目前 iPhone 没有分屏

纯代码, 不使用 StoryBoardxib

在 Xcode 11 和 iOS 13 中新增了 SceneDelegate, SceneDelegatescene 代替了 AppDelegatewindow

  • window 就是应用程序, 只有一个, 所有操作, 都在 window 中
  • scene 场景, 应用程序可以有多个场景, 每个场景都有一个 window

UISceneDelegate
Working with Window Scenes
Supporting Multiple Windows on iPad
Support side-by-side instances of your app’s interface and create new windows.
使用窗口场景
支持iPad上的多个窗口
支持应用界面的并行实例并创建新窗口。

AppDelegate (Xcode10 / iOS12 及之前)

AppDelegate 全权处理 App生命周期 和 UI生命周期

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    
    let nav = UINavigationController(rootViewController: ViewController())
    
    window = UIWindow(frame: UIScreen.main.bounds)
    window?.rootViewController = nav
    window?.makeKeyAndVisible()
    
    return true
}

// MARK: UIApplicationDelegate

@available(iOS 2.0, *)
optional func applicationDidBecomeActive(_ application: UIApplication)

@available(iOS 2.0, *)
optional func applicationWillResignActive(_ application: UIApplication)

@available(iOS 4.0, *)
optional func applicationDidEnterBackground(_ application: UIApplication)

@available(iOS 4.0, *)
optional func applicationWillEnterForeground(_ application: UIApplication)

AppDelegate + SceneDelegate (Xcode11 / iOS13)

  • AppDelegate 处理 App生命周期 和 SceneSession 生命周期
  • SceneSession 负责 UI生命周期

新项目变化

  • AppDelegate.swift 只配置 UISceneConfiguration, 不再管理 window,
// MARK: UISceneSession Lifecycle

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
    // Called when a new scene session is being created.
    // Use this method to select a configuration to create the new scene with.
    return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}

// 退出场景, 不会被重新连接
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
    // Called when the user discards a scene session.
    // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
    // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
  • 新增了 SceneDelegate.swift 文件, 管理场景的生命周期,处理各种响应
class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    // 与 didFinishLaunchingWithOptions 类似, 配置场景
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        
        guard let windowScene: UIWindowScene = (scene as? UIWindowScene) else { return }
        let window = UIWindow(windowScene: windowScene)
        self.window = window
        
        let nav = UINavigationController(rootViewController: ViewController())
        window.rootViewController = nav
        
        window.makeKeyAndVisible()
    }

    // 当场景与app断开连接 (还可能重新连接)
    func sceneDidDisconnect(_ scene: UIScene) {
        // Called as the scene is being released by the system.
        // This occurs shortly after the scene enters the background, or when its session is discarded.
        // Release any resources associated with this scene that can be re-created the next time the scene connects.
        // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
    }

    // 与 AppDelegate 的 applicationDidBecomeActive 类似
    func sceneDidBecomeActive(_ scene: UIScene) {
        // Called when the scene has moved from an inactive state to an active state.
        // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
    }

    // 与 AppDelegate 的 applicationWillResignActive 类似
    func sceneWillResignActive(_ scene: UIScene) {
        // Called when the scene will move from an active state to an inactive state.
        // This may occur due to temporary interruptions (ex. an incoming phone call).
    }

    // 与 AppDelegate 的 applicationWillEnterForeground 类似
    func sceneWillEnterForeground(_ scene: UIScene) {
        // Called as the scene transitions from the background to the foreground.
        // Use this method to undo the changes made on entering the background.
    }

    // 与 AppDelegate 的 applicationDidEnterBackground 类似
    func sceneDidEnterBackground(_ scene: UIScene) {
        // Called as the scene transitions from the foreground to the background.
        // Use this method to save data, release shared resources, and store enough scene-specific state information
        // to restore the scene back to its current state.
    }
}
  • Info.plist 新增了 Application Scene Manifest
<plist version="1.0">
<dict>
    <key>UIApplicationSceneManifest</key>
    <dict>
        <key>UIApplicationSupportsMultipleScenes</key>
        <false/>
        <key>UISceneConfigurations</key>
        <dict>
            <key>UIWindowSceneSessionRoleApplication</key>
            <array>
                <dict>
                    <key>UISceneConfigurationName</key>
                    <string>Default Configuration</string>
                    <key>UISceneDelegateClassName</key>
                    <string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
                    <key>UISceneStoryboardFile</key>
                    <string>Main</string>
                </dict>
            </array>
        </dict>
    </dict>
</dict>
</plist>

新项目删除 SceneDelegate

  1. 删除 SceneDelegate.swift
  2. 删除 Info.plist 新增了 UIApplicationSceneManifest
  3. 删除 AppDelegate.swift 中的 scene 相关方法
  4. 修改 LaunchScreen.storyboard 启动页面

有关[Swift] AppDelegate 与 SceneDelegate的更多相关文章

  1. swift - 将 json 编码时间转换为 nsdate - 2

    当我将time.Now()编码到JSON对象时,它给出的结果为"2009-11-10T23:00:00Z"但打印时间。现在给出2009-11-1023:00:00+0000UTC。他们为什么不同。什么是T和Z。另外,如何根据this将其转换为swiftNSDate对象?表? 最佳答案 这些值的含义无关紧要,它们是该格式(ISO8601)的一部分。有几种方法可以解决这个问题。一种是为时间或您的结构定义自定义MarshalJSON()方法并使用它来格式化日期,另一种是首先在您的结构中将其表示为字符串,以便当默认实现执行你得到你正在寻找的

  2. objective-c - 为什么 Swift 函数定义语法是多余的? - 2

    在C/C++/Java/Go中,我们使用,来分隔参数:(aint,bint)在ObjectiveC中,我们使用:来表示参数::(int)a:(int)b在Swift中,我们必须同时使用:和,:(a:int,b:int)是否需要冗余? 最佳答案 Swift可能有外部和内部参数名称:(externalinternal:Int)如果没有独特的分隔符,会产生很多歧义。 关于objective-c-为什么Swift函数定义语法是多余的?,我们在StackOverflow上找到一个类似的问题:

  3. swift - Swift 和 Go 之间的 Zlib 压缩 - 2

    我的Swift应用程序与用Go编写的服务器通信。我希望使用Zlib压缩传输的数据,但压缩结果似乎与Swift和Go不同。这是Go版本:sourceString:="A-t-ellebesoind'autrespreuves?Acceptez-lapourleplaisir.J'aitantfaitquedelacueillir,Etc'estpresqueunefleur-des-veuves."//Compressionvarbbytes.Bufferwriter:=zlib.NewWriter(&b)writer.Write([]byte(sourceString))writer.

  4. ios - swift api SecKeyCreateEncryptedData 使用的额外认证数据是什么? - 2

    我正在使用rsaEncryptionOAEPSHA256AESGCM在iOS上使用SecKeyCreateEncryptedData加密一些数据,然后在golang后端解密相同的数据。我正在使用3072位rsa公钥来加密对称key。当我从iOS获取数据到后端时,我能够成功解密对称key,但gcm标签验证失败。我使用的是与iOS相同的16字节IV,但不知道iOS在加密时是否使用任何aad(附加身份验证数据)。有谁知道rsaEncryptionOAEPSHA256AESGCMforiOS是否使用了一些aad?这适用于iOS10+。我已经尝试过使用nil、空的16字节数组、aeskey本身、

  5. json - 如何使用 iOS Swift 访问设备中的 vpn api url? - 2

    我使用swiftyJSON从apiurl消费OData。这里的apiurl与VPN连接。并且apiurl看起来像http://192.xxx.xx.xx:8000/sap/opu/odata/sap/Z_SRV/PRListSetSet?$format=json当我在模拟器中运行时,我可以从odataapiurl获取数据,但是在设备中运行时,没有从odataapiurl接收到数据。由于没有vpn连接到移动设备。我如何以编程方式对我的VPN进行硬编码以在移动设备中接收数据?这是我如何从ODataapiurl获取数据:typealiasServiceResponse=(JSON,Error

  6. xml - 使用 XML 解析 Swift 库时,Playgrounds 崩溃并显示 "unknown error" - 2

    我正在开发一个涉及一些XML解析的自定义框架,使用Kanna框架。每当我尝试将我的框架导入playground时,playground就会崩溃并出现以下错误:Playgroundexecutionfailed:expressionfailedtoparse,unknownerror*thread#1:tid=0x4e9448,0x00000001074bf360com.apple.dt.Xcode.PlaygroundStub-macosx`executePlayground,queue='com.apple.main-thread',stopreason=breakpoint1.1*

  7. swift - 在 Windows 10 上的 Ubuntu 上的 bash 上安装 swift 4 - 2

    我尝试在Windows10上的Ubuntu上的bash上安装Swift4我的Ubuntu版本:我@DESKTOP:~$lsb_release-a没有可用的LSB模块。经销商ID:Ubuntu描述:Ubuntu16.04.3LTS发布:16.04代号:xenial我做了apt-getupgrade和apt-getupdate我遵循Linux步骤here:安装Swift4最终我得到了错误:我@DESKTOP:~$swift/home/me/swift4/swift-4.0.2-RELEASE-ubuntu16.04/usr/bin/lldb:加载共享库时出错:libpython2.7.so

  8. c# - OneSignal:如何处理在 Xamarin.Forms 应用程序的 AppDelegate 中打开的通知? - 2

    我正致力于在Xamarin.Forms中实现OneSignal推送通知。我需要传递OneSignalAdditionalData返回的字符串进入App()的构造函数.所以我用了HandleNotificationOpened(OSNotificationOpenedResultresult)用于处理通知点击并获取字符串,然后将其传递给LoadApplication(newApp(myData)).为此,我将代码写在MainActivity中。适用于Android和AppDelegate适用于iOS。Android一切正常;即HandleNotificationOpened()获取了ad

  9. iOS-Swift 音视频采集与文件写入 - 2

    概述音视频采集是直播架构的第一步音视频采集包括两部分视频采集音频采集iOS开发中,同音视频采集相关API都封装在AVFoundation中,导入该框架,即可实现音频、视频的同步采集采集步骤采集步骤文字描述导入框架同采集相关API在AVFoundation中,因此需要先导入框架创建捕捉会话(AVCaptureSession)会话:用于连接输入源、输出源输入源:摄像头、麦克风输出源:对应的视频、音频数据设置视频输入源、输出源输入源(AVCaptureDeviceInput):从摄像头输入(前置/后置)输出源(AVCaptureVideoDataOutput):可从代理方法中拿到数据将输入源、输出源

  10. Swift 周报 第十六期 - 2

    前言本期是Swift编辑组自主整理周报的第七期,每个模块已初步成型。各位读者如果有好的提议,欢迎在文末留言。欢迎投稿或推荐内容。目前计划每两周周一发布,欢迎志同道合的朋友一起加入周报整理。当你来到双水村以外的大世界,你的人生目标便不单单是一名庄稼人了。Swift社区陪你一起成长,一起创造更多可能!👊👊👊周报精选新闻和社区:【挑战上岛】适配实时活动和灵动岛提案:函数反向部署Swift论坛:围绕Swift6lock展开的讨论推荐博文:推荐500+款AppUI设计工具推荐:妙言话题讨论:如果您年龄超过35岁被裁员,再入职时能接受降薪吗?新闻和社区挑战上岛:适配实时活动和灵动岛Apple大中华区设计与

随机推荐