{"id":4514,"date":"2021-12-10T16:48:11","date_gmt":"2021-12-10T08:48:11","guid":{"rendered":"http:\/\/123.57.164.21\/?p=4514"},"modified":"2022-01-14T17:43:56","modified_gmt":"2022-01-14T09:43:56","slug":"xcode-13-swiftui-%e5%b7%a5%e7%a8%8b%e7%9a%84%e4%b8%80%e4%ba%9btips","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=4514","title":{"rendered":"Xcode 13 SwiftUI \u5de5\u7a0b\u7684\u4e00\u4e9bTips"},"content":{"rendered":"\n<h5 class=\"wp-block-heading\"><meta charset=\"utf-8\">1.AppDelegate\u548c SceneDelegate<\/h5>\n\n\n\n<p>Xcode\u5347\u7ea7\u523013\u4ee5\u540e\uff0cInterface\u53ea\u6709SwiftUI \u548c Storyboard\u4e24\u4e2a\u9009\u9879\u4e86\u3002<\/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-174-1024x727.png\" alt=\"\" class=\"wp-image-4517\" width=\"451\" height=\"320\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-174-1024x727.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-174-300x213.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-174-768x545.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-174-830x590.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-174-230x163.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-174-350x249.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-174-480x341.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-174.png 1422w\" sizes=\"(max-width: 451px) 100vw, 451px\" \/><\/figure><\/div>\n\n\n\n<p>\u5efa\u597d\u4ee5\u540e\u7684\u5de5\u7a0b\uff0c\u53ef\u4ee5\u770b\u5230AppDelegate\u548c SceneDelegate \u548c info.plist, <meta charset=\"utf-8\">\u6211\u719f\u6089\u7684AppDelegate\u53bb\u54ea\u91cc\u4e86\uff1f<\/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-175-1024x594.png\" alt=\"\" class=\"wp-image-4518\" width=\"476\" height=\"276\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-175-1024x594.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-175-300x174.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-175-768x445.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-175-830x481.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-175-230x133.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-175-350x203.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-175-480x278.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-175.png 1314w\" sizes=\"(max-width: 476px) 100vw, 476px\" \/><\/figure><\/div>\n\n\n\n<p>\u591a\u51fa\u6765\u4e00\u4e2aDemoxxxApp\uff0c\u8fd9\u4e2a\u662f\u4ec0\u4e48\u9b3c\uff1f\uff0c\u8fd9\u54e5\u4eec\u53eb DemoxxxApp\uff08\u9879\u76ee\u540d\u79f0\uff09\uff0c \u662fSwiftUI\u90e8\u95e8\u7684\uff0c\u5e73\u65f6\u7684\u884c\u4e3a\u89c4\u8303\u662fApp\uff0c body\u662f\u4ed6\u4eec\u7684\u5de5\u88c5\uff0c\u51fa\u95e8\u5c31\u5f97\u7a7f\uff0c\u8fd9\u5de5\u88c5\u4e5f\u662f\u6709\u89c4\u8303\u7684\uff0c\u8fd9\u89c4\u8303\u53ebScene\u3002 \u6211\u539f\u6765\u719f\u6089\u7684UIKit\uff0cAppKit\uff0c\u5df2\u7ecf\u9000\u5c45\u4e8c\u7ebf\u4e86\uff0cApple\u6b63\u8ba1\u5212\u8ba9\u4ed6\u4eec\u9000\u4f11\u5462\u3002\u8fd9App\u554a\uff0c\u9876\u7684\u5c31\u662fUIApplicationDelegate\u7684\u73ed\u3002<\/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-176-1024x517.png\" alt=\"\" class=\"wp-image-4519\" width=\"498\" height=\"251\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-176-1024x517.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-176-300x151.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-176-768x387.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-176-830x419.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-176-230x116.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-176-350x177.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-176-480x242.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-176.png 1336w\" sizes=\"(max-width: 498px) 100vw, 498px\" \/><\/figure><\/div>\n\n\n\n<p>DemoxxxApp\u544a\u8bc9\u6211\uff0c\u4ed6\u4eec\u4e5f\u53ef\u4ee5\u6309\u7167UIApplicationDelegate\u7684\u65b9\u5f0f\u6765\u5de5\u4f5c\uff0c\u53ea\u4e0d\u8fc7\u9700\u8981\u6211\u989d\u5916\u505a\u4e00\u70b9\u5de5\u4f5c\u3002\u6211\u770b\u4e86\u4e0b\uff0c\u90a3\u5927\u6982\u5c31\u662f\u9700\u8981\u58f0\u660e\u4e00\u4e2a\u7b26\u5408UIApplicationDelegate\u534f\u8bae\u7684\u7c7b\uff0c\u7136\u540e\u4f7f\u7528@UIApplicationDelegateAdaptor\u7684\u6ce8\u89e3\u6807\u8bb0\u8be5\u7c7b\u5373\u53ef\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=\"\">import SwiftUI\n\nclass AppDelegate: NSObject, UIApplicationDelegate {\n    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {\n        print(\"log-didFinishLaunching\")\n        return true\n    }\n    func applicationDidReceiveMemoryWarning(_ application: UIApplication) {\n        print(\"log-DidReceiveMemoryWarning\")\n    }\n}\n\n@main\nstruct DemoxxxApp: App {\n    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate\n    var body: some Scene {\n        WindowGroup {\n            ContentView()\n        }\n    }\n}<\/pre>\n\n\n\n<p>SceneDelegate\u600e\u4e48\u529e\uff1f\u518d\u7ecf\u8fc7\u4e00\u756a\u6500\u8c08\u4ea4\u5fc3\uff0c\u4ed6\u544a\u8bc9\u6211\uff0c\u4e4b\u524dWindow\u90a3\u4e00\u5957\uff0c\u5df2\u7ecf\u4e0d\u7ba1\u7528\u4e86\uff0c\u73b0\u5728\u5f97\u7528Scene\u3002\u8bf4\u7740\uff0c\u7529\u51fa\u4e86\u4e00\u4efd<a rel=\"noreferrer noopener\" href=\"https:\/\/links.jianshu.com\/go?to=https%3A%2F%2Fdeveloper.apple.com%2Fdocumentation%2Fuikit%2Fapp_and_environment%2Fmanaging_your_app_s_life_cycle\" target=\"_blank\">\u6587\u6863<\/a>\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=\"\">When your app\u2019s state changes, UIKit notifies you by calling methods of the appropriate delegate object:\nIn iOS 13 and later, use UISceneDelegate objects to respond to life-cycle events in a scene-based app.\nIn iOS 12 and earlier, use the UIApplicationDelegate object to respond to life-cycle events.\n\nNote\nIf you enable scene support in your app, iOS always uses your scene delegates in iOS 13 and later. In iOS 12 and earlier, the system uses your app delegate.\n\n\u54e6\uff0c\u8fd9\u6587\u6863\u8bf4\u7684\u662f\uff0ciOS13\u4ee5\u524d\uff0c\u7531UIApplicationDelegate\u6765\u63a7\u5236\u58f0\u660e\u5468\u671f\uff0ciOS13\u4ee5\u540e\uff0c\u7531UISceneDelegate\u6765\u63a7\u5236\u58f0\u660e\u5468\u671f\u3002\u5728iOS 13\u4e4b\u540e\uff0c\u7528UIScene\u66ff\u4ee3\u4e86\u4e4b\u524dUIWindow\u6765\u7ba1\u7406\u89c6\u56fe\u3002\u4e3b\u8981\u662f\u4e3a\u4e86\u89e3\u51b3iPadOS\u5c55\u793a\u591a\u7a97\u53e3\u7684\u95ee\u9898\u3002<\/pre>\n\n\n\n<p>\u5728iOS 14\u4e4b\u540e\uff0cApple\u53c8\u7ed9SwiftUI\u63d0\u4f9b\u4e86\u66f4\u4f18\u96c5\u7684API\u6765\u663e\u793a\u548c\u63a7\u5236Scene\u3002\u6240\u4ee5\u63a7\u5236\u5e94\u7528\u5c55\u793a\u53ef\u4ee5\u8fd9\u6837\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 DemoxxxApp: App {\n    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate\n    @Environment(\\.scenePhase) var scenePhase\n    var body: some Scene {\n        WindowGroup {\n            ContentView()\n        }.onChange(of: scenePhase) { newScenePhase in\n            switch newScenePhase {\n            case .active:\n              print(\"\u5e94\u7528\u542f\u52a8\u4e86\")\n            case .inactive:\n              print(\"\u5e94\u7528\u4f11\u7720\u4e86\")\n            case .background:\n              print(\"\u5e94\u7528\u5728\u540e\u53f0\u5c55\u793a\")\n            @unknown default:\n              print(\"default\")\n            }\n        }\n    }\n}\n\n<\/pre>\n\n\n\n<p>\u53e6\u5916\uff1aopenURL\uff0cuserActivity\u7b49\u53ef\u4ee5\u76f4\u63a5\u6302\u5728\u67d0\u4e00\u4e2a\u5177\u4f53\u7684View\u4e0a\uff0c\u6765\u8fdb\u884c\u5355\u72ec\u5904\u7406\uff0c\u4e0d\u9700\u8981\u518d\u901a\u8fc7AppDelegate\u4e2d\u8f6c\u4e86\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e0b\u5c31\u641e\u5b9a\u4e86\uff0c\u53c8\u53ef\u4ee5\u5f00\u59cb\u6211\u6109\u5feb\u7684\u65c5\u7a0b\u4e86\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h5 class=\"wp-block-heading\">2.info.plist<\/h5>\n\n\n\n<p>\u8fd9\u4e2a\u6587\u4ef6\u5df2\u7ecf\u9ed8\u8ba4\u4e0d\u751f\u6210\u4e86\uff0c\u53ef\u4ee5\u5728info\u4e0b\u9762\u76f4\u63a5\u7f16\u8f91\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Projects created from several templates&nbsp;<strong>no longer require configuration files such as entitlements and Info.plist files<\/strong>. Configure common fields in the target\u2019s Info tab, and build settings in the project editor. These files are added to the project when additional fields are used. (68254857)<\/p><\/blockquote>\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-177-1024x79.png\" alt=\"\" class=\"wp-image-4522\" width=\"673\" height=\"51\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-177-1024x79.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-177-300x23.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-177-768x59.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-177-830x64.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-177-230x18.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-177-350x27.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-177-480x37.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-177.png 1324w\" sizes=\"(max-width: 673px) 100vw, 673px\" \/><\/figure><\/div>\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-178-1024x396.png\" alt=\"\" class=\"wp-image-4523\" width=\"655\" height=\"252\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-178-1024x396.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-178-300x116.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-178-768x297.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-178-830x321.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-178-230x89.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-178-350x136.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-178-480x186.png 480w\" sizes=\"(max-width: 655px) 100vw, 655px\" \/><\/figure><\/div>\n\n\n\n<h5 class=\"wp-block-heading\">3.WatchOS8\u4ee5\u540e\uff0c\u5de5\u7a0b\u91cc\u9762\u540c\u6837\u6ca1\u6709\u4e86ExtensionDelegate\u3002<\/h5>\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\n@main\nstruct xxxxxApp: App {\n    \n    @WKExtensionDelegateAdaptor(ExtensionDelegate.self) var extensionDelegate\n    \n    @Environment(\\.scenePhase) var scenePhase\n    @SceneBuilder var body: some Scene {\n        WindowGroup {\n            NavigationView {\n                ContentView()\n            }\n        }.onChange(of: scenePhase) { newScenePhase in\n            switch newScenePhase {\n            case .active:\n                print(\"active\")\n            case .inactive:\n                print(\"inactive\")\n            case .background:\n                print(\"background\")\n            @unknown default:\n                print(\"default\")\n            }\n        }\n\n        WKNotificationScene(controller: NotificationController.self, category: \"myCategory\")\n    }\n}\n\n\n\nimport WatchKit\n\nclass ExtensionDelegate: NSObject, WKExtensionDelegate, CLLocationManagerDelegate {\n    var locationManager: CLLocationManager?\n\n    func applicationDidFinishLaunching() {\n       \n    }\n\n    func applicationDidBecomeActive() {\n        \/\/ Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.\n    }\n\n    func applicationWillResignActive() {\n        \/\/ Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.\n        \/\/ Use this method to pause ongoing tasks, disable timers, etc.\n    }\n\n    func handle(_ backgroundTasks: Set&lt;WKRefreshBackgroundTask>) {\n        \/\/ Sent when the system needs to launch the application in the background to process tasks. Tasks arrive in a set, so loop through and process each one.\n        for task in backgroundTasks {\n            \/\/ Use a switch statement to check the task type\n            switch task {\n            case let backgroundTask as WKApplicationRefreshBackgroundTask:\n                \/\/ Be sure to complete the background task once you\u2019re done.\n                backgroundTask.setTaskCompletedWithSnapshot(false)\n            case let snapshotTask as WKSnapshotRefreshBackgroundTask:\n                \/\/ Snapshot tasks have a unique completion call, make sure to set your expiration date\n                snapshotTask.setTaskCompleted(restoredDefaultState: true, estimatedSnapshotExpiration: Date.distantFuture, userInfo: nil)\n            case let connectivityTask as WKWatchConnectivityRefreshBackgroundTask:\n                \/\/ Be sure to complete the connectivity task once you\u2019re done.\n                connectivityTask.setTaskCompletedWithSnapshot(false)\n            case let urlSessionTask as WKURLSessionRefreshBackgroundTask:\n                \/\/ Be sure to complete the URL session task once you\u2019re done.\n                urlSessionTask.setTaskCompletedWithSnapshot(false)\n            case let relevantShortcutTask as WKRelevantShortcutRefreshBackgroundTask:\n                \/\/ Be sure to complete the relevant-shortcut task once you're done.\n                relevantShortcutTask.setTaskCompletedWithSnapshot(false)\n            case let intentDidRunTask as WKIntentDidRunRefreshBackgroundTask:\n                \/\/ Be sure to complete the intent-did-run task once you're done.\n                intentDidRunTask.setTaskCompletedWithSnapshot(false)\n            default:\n                \/\/ make sure to complete unhandled task types\n                task.setTaskCompletedWithSnapshot(false)\n            }\n        }\n    }\n}<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">4.\u603b\u7ed3<\/h5>\n\n\n\n<ul><li>\u5728iOS13\u540e\uff0cScene\u53d6\u4ee3\u4e86Window\uff0c\u6765\u505a\u89c6\u56fe\u7684\u7ba1\u7406\u548c\u5448\u73b0\u3002<\/li><\/ul>\n\n\n\n<ul><li>\u5728SwiftUI\u4e2d\uff0c\u53ef\u4ee5\u63a7\u5236\u573a\u666f\u7684\u5207\u5165\uff0c\u5207\u51fa\uff08\u53d6\u4ee3applicationDidBecomeActive\uff0capplicationWillResignActive\uff0c applicationDidEnterBackground\uff09\uff0copenURL\uff0cuserActivity\u7b49\u4e8b\u4ef6\u3002<\/li><\/ul>\n\n\n\n<ul><li>SwiftUI\u73b0\u5728\u8fd8\u65e0\u6cd5\u5904\u7406applicationDidReceiveMemoryWarning\uff0capplicationWillTerminate\u7b49\u56de\u8c03\uff0c\u9700\u8981\u901a\u8fc7@UIApplicationDelegateAdaptor\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002<\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.AppDelegate\u548c SceneDelegate Xcode\u5347\u7ea7\u523013\u4ee5\u540e\uff0cInterface\u53ea\u6709Sw [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,7],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4514"}],"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=4514"}],"version-history":[{"count":4,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4514\/revisions"}],"predecessor-version":[{"id":4875,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4514\/revisions\/4875"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}