SwiftUI 属性包装器系列 --- @UIApplicationDelegateAdaptor @NSApplicationDelegateAdaptor

@UIApplicationDelegateAdaptor

SwiftUI iOS有了一个全新的生命周期,完全放弃了 UIKit 的应用程序和场景委托,但是有时需要使用UIKit 中UIApplicationDelegate的传统协议:处理推送通知的注册、响应内存警告、检测时间更改等,使用UIApplicationDelegate就可以实现。

如果需要访问SwiftUI iOS中AppDelegate的功能,您应该创建一个继承自NSObject 的类 UIApplicationDelegate 和协议的类,并为其提供功能。

1、实现AppDelegate协议 :创建一个继承NSObject并符合UIApplicationDelegate协议的类

swift 复制代码
class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        return true
    }
}

2、使用AppDelegate :在main->App-> Scene中使用UIApplicationDelegateAdaptor属性包装器

scss 复制代码
@main
struct YdtApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

UIApplicationDelegateAdaptor 与 ObservableObject 结合

1、让AppDelegate继承ObservableObject协议

swift 复制代码
class AppDelegate: NSObject, UIApplicationDelegate, ObservableObject {
    @Published var value: String = ""
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        self.value = "AppDelegate"
        return true
    }
}

2、通过环境委托(EnvironmentObject)属性包装器访问AppDelegate的值

scss 复制代码
struct ContentView: View {
    @EnvironmentObject var appDelegate: AppDelegate  
    var body: some View {
       Text("Value: (appDelegate.value)")  
    }
}

@NSApplicationDelegateAdaptor

在上面使用 @UIApplicationDelegateAdaptor 属性包装器来绑定 UIApplicationDelegate 类。但是UIApplicationDelegate 类在 macOS 上不可以使用,我们应该使用 @NSApplicationDelegateAdaptor属性包装器来实现功能

1、实现AppDelegate协议 NSApplicationDelegate NSWindowDelegate

swift 复制代码
private final class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
    var window: NSWindow?
    func applicationDidFinishLaunching(_ notification: Notification) {
        NSApp.delegate = self
        window = NSApplication.shared.windows[0]
        window?.isReleasedWhenClosed = false
        window?.delegate = self
    }

    func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
        window?.makeKeyAndOrderFront(self)
        return true
    }

    @objc func openMainWindow(_ sender: AnyObject?) {
        window?.orderFrontRegardless()
    }
}

注意:

一定要添加 NSApp.delegate = self,否则点击Dock图标无法进入applicationShouldHandleReopen方法,就会创建新的NsWindow

2、使用AppDelegate

scss 复制代码
@main
struct MyApp: App {
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
相关推荐
zhyongrui1 天前
SnipTrip 菜单 Liquid Glass 实现方案:结构、材质、交互与深浅色策略
ios·性能优化·swiftui·交互·开源软件·材质
zhyongrui1 天前
SnipTrip 不发烫的实现路径:局部刷新 + 合成缓存 + 峰值削减
ios·swiftui
初级代码游戏2 天前
iOS开发 SwiftUI 14:ScrollView 滚动视图
ios·swiftui·swift
初级代码游戏2 天前
iOS开发 SwitftUI 13:提示、弹窗、上下文菜单
ios·swiftui·swift·弹窗·消息框
zhyongrui2 天前
托盘删除手势与引导体验修复:滚动冲突、画布消失动画、气泡边框
ios·性能优化·swiftui·swift
zhangfeng11332 天前
CSDN星图 支持大模型微调 trl axolotl Unsloth 趋动云 LLaMA-Factory Unsloth ms-swift 模型训练
服务器·人工智能·swift
zhyongrui3 天前
SnipTrip 发热优化实战:从 60Hz 到 30Hz 的性能之旅
ios·swiftui·swift
大熊猫侯佩4 天前
Neo-Cupertino 档案:撕开 Actor 的伪装,回归 Non-Sendable 的暴力美学
swift·observable·actor·concurrency·sendable·nonsendable·data race
大熊猫侯佩4 天前
赛博深渊(上):用 Apple Foundation Models 提炼“禁忌知识”的求生指南
llm·swiftui·大语言模型·foundationmodel·apple ai·apple 人工智能·summarize
zhyongrui5 天前
SwiftUI 光晕动画性能优化:消除托盘缩放卡顿的实战方案
ios·性能优化·swiftui