{"id":4772,"date":"2021-12-25T21:42:31","date_gmt":"2021-12-25T13:42:31","guid":{"rendered":"http:\/\/123.57.164.21\/?p=4772"},"modified":"2021-12-25T21:42:31","modified_gmt":"2021-12-25T13:42:31","slug":"swiftui2-0-app%e3%80%81scene%e3%80%81%e6%96%b0%e7%9a%84%e4%bb%a3%e7%a0%81%e7%bb%93%e6%9e%84%ef%bc%88%e4%b8%80%ef%bc%89","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=4772","title":{"rendered":"SwiftUI2.0 \u2014\u2014 App\u3001Scene\u3001\u65b0\u7684\u4ee3\u7801\u7ed3\u6784\uff08\u4e00\uff09"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1ahttps:\/\/www.fatbobman.com\/posts\/swiftui2-new-feature-1\/<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u672c\u6587\u7b80\u5355\u4ecb\u7ecd\u4e86 SwiftUI2.0 \u4e2d\u5168\u65b0\u63d0\u4f9b\u7684 App \u534f\u8bae\u3001Scene \u534f\u8bae\uff0c\u6d45\u8c08\u4e86\u5728\u5168\u65b0\u7684\u4ee3\u7801\u7ed3\u6784\u4e0b\u5982\u4f55\u7ec4\u7ec7 Data Flow\uff0c\u5e76\u63d0\u4f9b\u4e86 SwiftUI2.0 \u4e2d\u9884\u7f6e\u7684 Scene \u7684\u4e00\u4e9b\u4f7f\u7528\u793a\u4f8b\u3002 \u5f53\u524d\u8fd0\u884c\u73af\u5883\u4e3a Xcode Version 12.0 beta (12A6159), macOS Big Sur 11.0 Beta \u7248 (20A4299v)\u3002<\/p><\/blockquote>\n\n\n\n<p>WWDC20 \u4e2d\uff0c\u82f9\u679c\u4e3a\u5f00\u53d1\u8005\u5e26\u6765\u4e86\u57fa\u4e8e SwiftUI \u7684\u5168\u65b0\u9879\u76ee\u6a21\u677f\u3002\u4f7f\u7528\u8be5\u6a21\u677f\uff0c\u5c06\u4f7f\u9879\u76ee\u4ee3\u7801\u53d8\u5f97\u5f02\u5e38\u7b80\u6d01\u3001\u6e05\u6670\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@main\nstruct NewAllApp: App {\n    var body: some Scene {\n        WindowGroup {\n            Text(\"Hello world\")\n        }\n    }\n}<\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u4ee3\u7801\u53ef\u4ee5\u5728\u5c4f\u5e55\u4e0a\u5b8c\u6210 Hello world \u7684\u663e\u793a\uff0c\u4e14\u80fd\u591f\u8fd0\u884c\u4e8e iOS \u548c macOS \u5e73\u53f0\u4e0b\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u57fa\u672c\u6982\u5ff5<\/h5>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p id=\"App\"><strong>App<\/strong><\/p>\n\n\n\n<p>SwiftUI2.0 \u63d0\u4f9b\u7684\u5168\u65b0\u534f\u8bae\u3002\u901a\u8fc7\u58f0\u660e\u4e00\u4e2a\u7b26\u5408 App \u534f\u8bae\u7684\u7ed3\u6784\u6765\u521b\u5efa\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u5e76\u901a\u8fc7\u8ba1\u7b97\u5c5e\u6027 body \u6765\u5b9e\u73b0\u7a0b\u5e8f\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<ul><li>\u901a\u8fc7@main(swift5.3 \u65b0\u7279\u6027\uff09\u8bbe\u5b9a\u7a0b\u5e8f\u7684\u5165\u53e3\uff0c\u6bcf\u4e2a\u9879\u76ee\u53ea\u80fd\u6709\u4e00\u4e2a\u8fdb\u5165\u70b9<\/li><li>\u7ba1\u7406\u6574\u4e2a app \u7684\u751f\u547d\u5468\u671f<\/li><li>\u5728\u8fd9\u4e2a\u4f5c\u7528\u57df\u4e0b\u58f0\u660e\u7684\u5e38\u91cf\u3001\u53d8\u91cf\u5176\u751f\u547d\u5468\u671f\u4e0e\u6574\u4e2a app \u662f\u5b8c\u5168\u4e00\u81f4\u7684\u3002<\/li><\/ul>\n\n\n\n<p id=\"Scene\"><strong>Scene<\/strong><\/p>\n\n\n\n<p>\u573a\u666f\u662f\u89c6\u56fe\uff08View\uff09\u5c42\u6b21\u7ed3\u6784\u7684\u5bb9\u5668\u3002\u901a\u8fc7\u5728 App \u5b9e\u4f8b\u7684 body \u4e2d\u7ec4\u5408\u4e00\u4e2a\u6216\u591a\u4e2a\u7b26\u5408 Scene \u534f\u8bae\u7684\u5b9e\u4f8b\u6765\u5448\u73b0\u5177\u4f53\u7a0b\u5e8f\u3002<\/p>\n\n\n\n<ul><li>\u751f\u547d\u5468\u671f\u7531\u7cfb\u7edf\u7ba1\u7406<\/li><li>\u7cfb\u7edf\u4f1a\u6839\u636e\u8fd0\u884c\u5e73\u53f0\u7684\u4e0d\u540c\u800c\u8c03\u6574\u573a\u666f\u7684\u5c55\u793a\u884c\u4e3a\uff08\u6bd4\u5982\u76f8\u540c\u7684\u4ee3\u7801\u5728 iOS \u548c macOS \u4e0b\u7684\u5448\u73b0\u4e0d\u540c\uff0c\u6216\u8005\u67d0\u4e9b\u573a\u666f\u4ec5\u80fd\u8fd0\u884c\u4e8e\u7279\u5b9a\u7684\u5e73\u53f0\uff09<\/li><li>SwiftUI2.0 \u63d0\u4f9b\u4e86\u51e0\u4e2a\u9884\u7f6e\u7684\u573a\u666f\uff0c\u7528\u6237\u4e5f\u53ef\u4ee5\u81ea\u5df1\u7f16\u5199\u7b26\u5408 Scene \u534f\u8bae\u7684\u573a\u666f\u3002\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u4fbf\u662f\u4f7f\u7528\u7684\u4e00\u4e2a\u9884\u7f6e\u573a\u666f WindowGroup<\/li><\/ul>\n\n\n\n<p>\u901a\u8fc7 App \u548c Scene \u7684\u52a0\u5165\uff0c\u7edd\u4e0d\u662f\u4ec5\u4ec5\u51cf\u5c11\u4ee3\u7801\u91cf\u8fd9\u4e48\u7b80\u5355\u3002\u901a\u8fc7\u8fd9\u4e2a\u660e\u786e\u7684\u5c42\u7ea7\u8bbe\u5b9a\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u7684\u638c\u63e1\u5728\u4e0d\u540c\u4f5c\u7528\u57df\u4e0b\u5404\u4e2a\u90e8\u5206\u7684\u751f\u547d\u5468\u671f\u3001\u66f4\u7cbe\u51c6\u6570\u636e\u4f20\u9012\u3001\u4ee5\u53ca\u66f4\u4fbf\u5229\u7684\u591a\u5e73\u53f0\u4ee3\u7801\u5171\u4eab\u3002\u672c\u6587\u540e\u9762\u4f1a\u7528\u5177\u4f53\u4ee3\u7801\u6765\u9010\u4e2a\u9610\u8ff0\u3002<\/p>\n\n\n\n<p><em>App \u548c Scene \u90fd\u662f\u901a\u8fc7\u5404\u81ea\u7684 functionBuilder \u6765\u89e3\u6790\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u65b0\u7684\u6a21\u677f\u4ece\u7a0b\u5e8f\u7684\u5165\u53e3\u5f00\u59cb\u4fbf\u662f\u4f7f\u7528 DSL \u6765\u63cf\u8ff0\u7684\u3002<\/em><\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u7a0b\u5e8f\u7cfb\u7edf\u4e8b\u4ef6\u54cd\u5e94<\/h5>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u7531\u4e8e\u53bb\u9664\u4e86 AppDelegate.swift \u548c SceneDelegate.swift\uff0cSwiftUI2.0 \u63d0\u4f9b\u4e86\u65b0\u7684\u65b9\u6cd5\u6765\u8ba9\u7a0b\u5e8f\u54cd\u5e94\u7cfb\u7edf\u4e8b\u4ef6\u3002<\/p>\n\n\n\n<p id=\"\u9488\u5bf9_AppDelegate.swift\"><strong>\u9488\u5bf9 AppDelegate.swift<\/strong><\/p>\n\n\n\n<p>\u5728 iOS \u7cfb\u7edf\u4e0b\uff0c\u901a\u8fc7\u4f7f\u7528@UIApplicationDelegateAdaptor \u53ef\u4ee5\u65b9\u4fbf\u7684\u5b9e\u73b0\u4e4b\u524d AppDelegate.swfit \u4e2d\u63d0\u4f9b\u7684\u529f\u80fd\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@main\nstruct NewAllApp: App {\n   @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate\n    var body: some Scene {\n        WindowGroup {\n            Text(\"Hello world\")\n        }\n    }\n}\n\nclass AppDelegate:NSObject,UIApplicationDelegate{\n    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {\n        print(\"launch\")\n        return true\n    }\n}\n<\/pre>\n\n\n\n<p>\u7531\u4e8e\u76ee\u524d\u8fd8\u662f\u6d4b\u8bd5\u7248\uff0c\u867d\u7136\u5f88\u591a\u7684\u4e8b\u4ef6\u5df2\u7ecf\u5b9a\u4e49\uff0c\u4f46\u73b0\u5728\u5e76\u6ca1\u6709\u54cd\u5e94\u3002\u4f30\u8ba1\u5f88\u5feb\u4f1a\u589e\u52a0\u4fee\u6539\u8fc7\u6765<\/p>\n\n\n\n<p id=\"\u9488\u5bf9_SceneDelegate.swift\"><strong>\u9488\u5bf9 SceneDelegate.swift<\/strong><\/p>\n\n\n\n<p>\u901a\u8fc7\u65b0\u589e\u6dfb\u7684 EnvironmentKey\u00a0<strong>scenePhase<\/strong>\u00a0\u548c\u65b0\u7684<strong>.onChange<\/strong>\u00a0\u65b9\u6cd5\uff0cSwiftUI \u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u52a0\u6709\u8da3\u7684\u573a\u666f\u4e8b\u4ef6\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@main\nstruct NewAllApp: App {\n    @Environment(\\.scenePhase) var phase\n    var body: some Scene {\n        WindowGroup {\n           ContentView()\n        }\n        .onChange(of: phase){phase in\n                switch phase{\n                case .active:\n                    print(\"active\")\n                case .inactive:\n                    print(\"inactive\")\n                case .background:\n                    print(\"background\")\n                @unknown default:\n                    print(\"for future\")\n                }\n\n          }\n    }\n}<\/pre>\n\n\n\n<p>\u540c\u6837\u662f\u7531\u4e8e\u6d4b\u8bd5\u7248\u7684\u539f\u56e0\uff0c\u8be5\u54cd\u5e94\u76ee\u524d\u5e76\u6ca1\u6709\u5b8c\u6210\u3002\u4e0d\u8fc7\u8fd9\u6bb5\u4ee3\u7801\u76ee\u524d\u6765\u770b\u662f iOS \u548c macOS \u90fd\u901a\u7528\u7684<\/p>\n\n\n\n<p id=\"\u66f4\u65b0\"><strong>\u66f4\u65b0<\/strong><\/p>\n\n\n\n<p>\u76ee\u524d\u53d1\u73b0\u5982\u679c\u5728 View \u4e2d\uff0c\u53ef\u4ee5\u83b7\u53d6 scenePhase \u7684\u72b6\u6001\u66f4\u65b0\u3002\u4e0b\u6765\u4ee3\u7801\u76ee\u524d\u53ef\u4ee5\u6b63\u5e38\u6267\u884c<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct ContentView:View{ \n  @Environment(\\.scenePhase) private var scenePhase\n var body: some Scene {\n  WindowGroup {\n   ContentView()\n  }\n  .onChange(of: phase){phase in\n                switch phase{\n                case .active:\n                    print(\"active\")\n                case .inactive:\n                    print(\"inactive\")\n                case .background:\n                    print(\"background\")\n                @unknown default:\n                    print(\"for future\")\n                }\n         }\n   }\n}<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u9884\u7f6e\u573a\u666f<\/h5>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ul><li><strong>WKNotificationScene<\/strong>&nbsp;\u4ec5\u9002\u7528\u4e8e watchOS7.0\uff0c\u7528\u4e8e\u54cd\u5e94\u6307\u5b9a\u7c7b\u522b\u7684\u8fdc\u7a0b\u6216\u672c\u5730\u901a\u77e5\u3002\u76ee\u524d\u8fd8\u6ca1\u6709\u7814\u7a76\u3002<\/li><li><strong>WindowGroup<\/strong><\/li><\/ul>\n\n\n\n<p>\u6700\u5e38\u7528\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u5448\u73b0\u4e00\u7ec4\u7ed3\u6784\u76f8\u540c\u7684\u7a97\u53e3\u3002\u4f7f\u7528\u8be5\u573a\u666f\uff0c\u6211\u4eec\u65e0\u9700\u5728\u4ee3\u7801\u4e0a\u505a\u4fee\u6539\uff0c\u53ea\u9700\u8981\u5728\u9879\u76ee\u4e2d\u8bbe\u5b9a\u662f\u5426\u652f\u6301\u591a\u7a97\u53e3\uff0c\u7cfb\u7edf\u5c06\u4f1a\u6309\u7167\u8fd0\u884c\u5e73\u53f0\u7684\u7279\u6027\u81ea\u52a8\u7ba1\u7406\u3002<\/p>\n\n\n\n<p>\u5728 iOS \u4e2d\uff0c\u53ea\u80fd\u5448\u73b0\u4e00\u4e2a\u8fd0\u884c\u7a97\u53e3\u3002<\/p>\n\n\n\n<p>\u5728 PadOS \u4e2d\uff08\u5982\u6253\u5f00\u591a\u7a97\u53e3\u652f\u6301\uff09\uff0c\u6700\u591a\u53ef\u4ee5\u6253\u5f00\u4e24\u4e2a\u8fd0\u884c\u7a97\u53e3\uff0c\u53ef\u4ee5\u5206\u5c4f\u663e\u793a\uff0c\u4e5f\u53ef\u4ee5\u5168\u5c4f\u72ec\u7acb\u663e\u793a\u3002<\/p>\n\n\n\n<p>\u5728 macOS \u4e2d\uff0c\u53ef\u4ee5\u6253\u5f00\u591a\u4e2a\u7a97\u53e3\uff0c\u5e76\u901a\u8fc7\u7a0b\u5e8f\u83dc\u5355\u4e2d\u7684\u7a97\u53e3\u83dc\u5355\u6765\u8fdb\u884c\u591a\u7a97\u53e3\u7ba1\u7406\u3002<\/p>\n\n\n\n<p>\u6700\u5f00\u59cb\u7684\u4ee3\u7801\u5728\u4e09\u4e2a\u5e73\u53f0\u4e0b\u7684\u72b6\u6001\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/12\/image-256-1024x1019.png\" alt=\"\" class=\"wp-image-4773\" width=\"426\" height=\"423\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-256-1024x1019.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-256-150x150.png 150w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-256-768x764.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-256-830x826.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-256-230x229.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-256-350x348.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-256-480x478.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-256.png 1270w\" sizes=\"(max-width: 426px) 100vw, 426px\" \/><\/figure><\/div>\n\n\n\n<p>\u5982\u679c\u5728\u4e00\u4e2a WindowGroup \u91cc\u52a0\u5165\u591a\u4e2a View, \u5448\u73b0\u72b6\u6001\u6709\u70b9\u7c7b\u4f3c VStack\u3002<\/p>\n\n\n\n<p>\u5728\u4e00\u4e2a Scene \u4e2d\u52a0\u5165\u591a\u4e2a WindowGroup\uff0c\u53ea\u6709\u6700\u524d\u9762\u7684\u53ef\u4ee5\u88ab\u663e\u793a\u3002<\/p>\n\n\n\n<ul><li><strong>DocumentGroup<\/strong><\/li><\/ul>\n\n\n\n<p>\u521b\u5efa\u4e00\u4e2a\u53ef\u5904\u7406\u6307\u5b9a\u6587\u4ef6\u7c7b\u578b\u7684\u7a97\u53e3\u3002\u5728 iOS \u548c PadOS \u4e0b\u90fd\u9996\u5148\u4f1a\u5448\u73b0\u6587\u4ef6\u7ba1\u7406\u5668\uff0c\u70b9\u51fb\u6587\u4ef6\uff0c\u8fdb\u5165\u5bf9\u5e94\u7684 View \u6765\u5904\u7406\u3002macOS \u4e0b\uff0c\u901a\u8fc7\u83dc\u5355\u4e2d\u7684\u6587\u4ef6\u64cd\u4f5c\u6765\u9009\u62e9\u6216\u521b\u5efa\u6587\u4ef6\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u7b26\u5408 FileDocument \u7684\u7ed3\u6784\u6765\u5b9a\u4e49\u652f\u6301\u54ea\u79cd\u683c\u5f0f\uff0c\u4ee5\u53ca\u6253\u5f00\u548c\u4fdd\u5b58\u7684\u5de5\u4f5c\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/\u7eaf\u6587\u672c\u683c\u5f0f\u6587\u4ef6\u3002write \u7684\u65b9\u6cd5\u7528\u4e8e\u63cf\u8ff0\u5982\u4f55\u5199\u5165\u6587\u4ef6\uff0c\u5982\u679c\u4e0d\u9700\u5199\u5165\u53ef\u4e3a\u7a7a\u3002\n  struct TextFile: FileDocument {\n      static var readableContentTypes = [UTType.plainText]\n      var text = \"\"\n      init(initialText: String = \"\") {\n          text = initialText\n      }\n      init(fileWrapper: FileWrapper, contentType: UTType) throws {\n          if let data = fileWrapper.regularFileContents {\n              text = String(decoding: data, as: UTF8.self)\n          }\n      }\n      func write(to fileWrapper: inout FileWrapper, contentType: UTType) throws {\n          let data = Data(text.utf8)\n          let file = FileWrapper(regularFileWithContents: data)\n          fileWrapper = file\n      }\n  }\n  \/\/\u56fe\u7247\u6587\u4ef6\uff0c\u7531\u4e8e\u9700\u8981\u8f6c\u6362\u6210 UIImage\uff0c\u8be5\u4ee3\u7801\u53ea\u652f\u6301 iOS \u6216 PadOS\n  #if os(iOS)\n  struct ImageFile: FileDocument {\n      static var readableContentTypes = [UTType.image]\n      var image = UIImage()\n      init(initialImage: UIImage = UIImage()) {\n          image = initialImage\n      }\n    \n      init(fileWrapper: FileWrapper, contentType: UTType) throws {\n          if let data = fileWrapper.regularFileContents {\n              image =   UIImage(data: data) ?? UIImage()\n          }\n      }\n  \n      func write(to fileWrapper: inout FileWrapper, contentType: UTType) throws { }\n  }\n  #endif<\/pre>\n\n\n\n<p>\u8c03\u7528<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">  import SwiftUI\n  #if os(iOS)\n  import UIKit\n  #endif\n  import UniformTypeIdentifiers\n  \n  @main\n  struct NewAllApp: App {\n     @SceneBuilder var body: some Scene {\n          \/\/\u53ef\u8bfb\u5199\n          DocumentGroup(newDocument: TextFile()) { file in\n                  TextEditorView(document: file.$document)\n          }\n          \n          #if os(iOS)\n          \/\/\u53ea\u8bfb\n          DocumentGroup(viewing: ImageFile.self) { file in\n                  ImageViewerView(file: file.$document)\n            }\n          #endif\n      }\n  }\n  \n  struct TextEditorView: View {\n      @Binding var document: TextFile\n      @State var name = \"\"\n      var body: some View {\n          VStack{\n          TextEditor(text: $document.text)\n              .padding()\n          }\n          .background(Color.gray)\n      }\n  }\n  \n  #if os(iOS)\n  struct ImageViewerView:View{\n      @Binding var document:ImageFile\n      var body: some View{\n          Image(uiImage: document.image)\n              .resizable(resizingMode: .stretch)\n              .aspectRatio(contentMode: .fit)\n      }\n  }\n  #endif\n<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/12\/image-257-1022x1024.png\" alt=\"\" class=\"wp-image-4775\" width=\"552\" height=\"552\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-257-1022x1024.png 1022w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-257-300x300.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-257-150x150.png 150w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-257-768x769.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-257-830x831.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-257-230x230.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-257-350x351.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-257-480x481.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-257.png 1220w\" sizes=\"(max-width: 552px) 100vw, 552px\" \/><\/figure><\/div>\n\n\n\n<p>\u53ef\u4ee5\u5c06\u591a\u4e2a DocumentGroup \u653e\u5165 Scene \u4e2d\uff0c\u7a0b\u5e8f\u5c06\u4f1a\u4e00\u5e76\u652f\u6301\u6bcf\u4e2a DocumentGroup \u6240\u5b9a\u4e49\u7684\u6587\u4ef6\u7c7b\u578b\u3002\u4e0a\u8ff0\u4ee3\u7801\u4f7f\u7a0b\u5e8f\u53ef\u4ee5\u521b\u5efa\u3001\u7f16\u8f91\u7eaf\u6587\u672c\u6587\u4ef6\uff0c\u5e76\u4e14\u53ef\u4ee5\u6d4f\u89c8\u56fe\u7247\u6587\u4ef6\u3002<\/p>\n\n\n\n<p>\u5728 macOS \u4e0a\uff0c\u9700\u8981\u5728 macOS.entitlements \u4e2d\u8bbe\u7f6e com.apple.security.files.user-selected.read-write \u4e3a\u771f\u624d\u80fd\u5b8c\u6210\u5199\u5165\u3002<\/p>\n\n\n\n<p>\u5f53\u5728 Scene \u4e2d\u52a0\u5165\u591a\u4e2a\u573a\u666f\u65f6\uff0c\u9700\u8981\u4f7f\u7528@SceneBuilder \u6216\u7528 Group \u5c06\u591a\u4e2a\u573a\u666f\u6db5\u76d6\u8d77\u6765\u3002<\/p>\n\n\n\n<p>macOS \u4e0b\u5f53\u540c\u65f6\u52a0\u5165 WindowGroup \u548c DocumentGroup \u65f6\uff0c\u4e24\u4e2a\u529f\u80fd\u90fd\u53ef\u4ee5\u6b63\u5e38\u8fd0\u884c\u3002iOS \u6216 PadOS \u4e0b\uff0c\u53ea\u6709\u987a\u5e8f\u5728\u6700\u524d\u9762\u7684\u88ab\u663e\u793a\u3002<\/p>\n\n\n\n<p>\u7531\u4e8e\u6d4b\u8bd5\u7248\u7684\u539f\u56e0\uff0c\u76ee\u524d\u4ecd\u6709\u5927\u91cf\u7684\u529f\u80fd\u65e0\u6cd5\u5b9e\u73b0\u6216\u6709\u95ee\u9898\u3002\u6bd4\u5982\u4ecd\u65e0\u6cd5\u5728 iOS \u4e0a\u901a\u8fc7 fileDocument \u63d0\u4f9b\u7684 filename \u6765\u8bbe\u7f6e\u6587\u4ef6\u540d\uff0c\u6216\u8005\u65e0\u6cd5\u5728\u521b\u5efa\u65b0\u6587\u4ef6\u65f6\u9009\u62e9\u683c\u5f0f\u7b49<\/p>\n\n\n\n<ul><li><strong>Settings<\/strong><\/li><\/ul>\n\n\n\n<p>\u53ea\u7528\u4e8e macOS, \u7528\u4e8e\u7f16\u5199\u7a0b\u5e8f\u7684\u504f\u597d\u8bbe\u7f6e\u7a97\u53e3\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#if os(macOS)\n      Settings{\n        Text(\"\u504f\u597d\u8bbe\u7f6e\").padding(.all, 50)\n      }\n  #endif<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/12\/image-258-1024x574.png\" alt=\"\" class=\"wp-image-4776\" width=\"497\" height=\"278\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-258-1024x574.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-258-300x168.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-258-768x431.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-258-830x465.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-258-230x129.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-258-350x196.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-258-480x269.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-258.png 1238w\" sizes=\"(max-width: 497px) 100vw, 497px\" \/><\/figure><\/div>\n\n\n\n<h5 class=\"wp-block-heading\">\u5176\u4ed6<\/h5>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ul><li><strong>onChange<\/strong>&nbsp;\u76d1\u89c6\u6307\u5b9a\u7684\u503c\uff0c\u5728\u503c\u6539\u53d8\u65f6\u6267\u884c\u6307\u5b9a\u7684 action\u3002\u5728 scenePhase \u7684\u7528\u6cd5\u4ecb\u7ecd\u4e2d\u6709\u4f7f\u7528\u7684\u8303\u4f8b<\/li><li><strong>onCommands<\/strong>&nbsp;\u5728 macOS \u4e0b\u8bbe\u7f6e\u7a0b\u5e8f\u7684\u83dc\u5355\u3002\u5177\u4f53\u7684\u4f7f\u7528\u65b9\u6cd5\u8bf7\u67e5\u770b&nbsp;<a href=\"https:\/\/www.fatbobman.com\/posts\/swiftUI2-commands\/\">SwiftUI2.0 \u2014\u2014 Commands\uff08macOS \u83dc\u5355\uff09<\/a><\/li><li><strong>defaultAppStorage<\/strong>&nbsp;\u5982\u679c\u4e0d\u60f3\u4f7f\u7528\u7cfb\u7edf\u7f3a\u7701 UserDefault.standard\uff0c\u53ef\u4ee5\u81ea\u884c\u8bbe\u7f6e\u5b58\u50a8\u4f4d\u7f6e\uff0c\u4f7f\u7528\u7684\u51e0\u7387\u4e0d\u9ad8\u3002<\/li><\/ul>\n\n\n\n<h5 class=\"wp-block-heading\">\u5c0f\u7ed3<\/h5>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u81f3\u6b64\uff0c\u672c\u6587\u7b80\u5355\u4ecb\u7ecd\u4e86 SwiftUI2.0 \u65b0\u589e\u7684 App \u548c Scene\uff0c\u4e0b\u7bc7\u6587\u7ae0\u6211\u4eec\u5c06\u63a2\u8ba8\u5728\u65b0\u7684\u5c42\u6b21\u7ed3\u6784\u4e0b\u5982\u4f55\u7ec4\u7ec7\u6211\u4eec\u4ee3\u7801\u7684 Data Flow\u3002<\/p>\n\n\n\n<p>\u5f53\u524d\u7684@AppBuilder \u548c@SceneBuilder \u7684\u529f\u80fd\u90fd\u5341\u5206\u7684\u57fa\u7840\uff0c\u4e0d\u5305\u542b\u4efb\u4f55\u7684\u903b\u8f91\u5224\u65ad\u529f\u80fd\uff0c\u56e0\u6b64\u76ee\u524d\u6211\u8fd8\u6ca1\u6709\u529e\u6cd5\u5b9e\u73b0\u6839\u636e\u6761\u4ef6\u6765\u9009\u62e9\u6027\u7684\u5c55\u793a\u6240\u9700\u7684 Scene\u3002\u76f8\u4fe1\u82f9\u679c\u5e94\u8be5\u4f1a\u5728\u672a\u6765\u589e\u52a0\u8fd9\u6837\u7684\u80fd\u529b<\/p>\n\n\n\n<p><em>\u672c\u6587\u7684\u4ee3\u7801\u4e3a\u4e86\u80fd\u591f\u5728\u591a\u5e73\u53f0\u4f7f\u7528\uff0c\u6240\u4ee5\u589e\u52a0\u4e86\u4e0d\u5c11\u7f16\u8bd1\u5224\u65ad\uff0c\u5982\u679c\u4f60\u53ea\u662f\u5728 iOS, \u6216 macOS \u4e0b\u5f00\u53d1 SwiftUI\uff0c\u5219\u53ef\u6839\u636e\u5404\u81ea\u5e73\u53f0\u7b80\u5316\u4ee3\u7801\u3002\u53e6\u5916 Xcode12 \u4e2d\u7684\u4ee3\u7801\u8865\u5168\u5bf9\u4e8e Target \u7684\u8bbe\u5b9a\u5f88\u654f\u611f\uff0c\u5982\u679c\u4f60\u53d1\u73b0\u65e0\u6cd5\u5bf9\u67d0\u4e9b\u5e73\u53f0\u7684\u7279\u5b9a\u8bed\u53e5\u8fdb\u884c\u8865\u5168\uff0c\u8bf7\u67e5\u770b\u662f\u5426\u5c06 Scheme \u8bbe\u7f6e\u5230\u5bf9\u5e94\u7684\u5e73\u53f0\u3002<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1ahttps:\/\/www.fatbobman.com\/posts\/swiftui2-new-feature [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4772"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4772"}],"version-history":[{"count":2,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4772\/revisions"}],"predecessor-version":[{"id":4777,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4772\/revisions\/4777"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}