{"id":254,"date":"2023-10-20T13:54:42","date_gmt":"2023-10-20T05:54:42","guid":{"rendered":"https:\/\/www.nickyoo7.com\/?p=254"},"modified":"2023-10-20T14:04:23","modified_gmt":"2023-10-20T06:04:23","slug":"ios-%e5%bb%ba%e7%ab%8b%e9%81%a0%e8%b7%9d%e6%8e%a8%e6%92%ad-app-%e4%b8%80-swift-%e7%a8%8b%e5%bc%8f%e7%af%87","status":"publish","type":"post","link":"https:\/\/www.nickyoo7.com\/?p=254","title":{"rendered":"iOS \u5efa\u7acb\u9060\u8ddd\u63a8\u64ad App (\u4e00) Swift \u7a0b\u5f0f\u7bc7"},"content":{"rendered":"<p>iOS \u5efa\u7acb\u9060\u8ddd\u63a8\u64ad App (\u4e00) Swift \u7a0b\u5f0f\u7bc7<br \/>\n<!--more--><br \/>\nStep 01 : \u5efa\u7acb Storyboard \u5c08\u6848<br \/>\nStep 02 : \u8a2d\u5b9a\u5404\u9805\u53c3\u6578<br \/>\n(1) \u5728 \u5c08\u6848(Project)\u00a0 TARGETS \u9801 Signing &amp; Capabilities \u5167\u65b0\u589e<br \/>\nBackground Modes \u53ca Push Notification \u4e8c\u500b Capabilities<br \/>\nBackground Modes \u9805\u76ee\u52fe\u9078 Remote notifications<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-255\" src=\"https:\/\/www.nickyoo7.com\/wp-content\/uploads\/2023\/10\/\u9060\u7aef\u63a8\u64ad-xcode-\u8a2d\u5b9a-Swift.png\" alt=\"\" width=\"1368\" height=\"918\" srcset=\"https:\/\/www.nickyoo7.com\/wp-content\/uploads\/2023\/10\/\u9060\u7aef\u63a8\u64ad-xcode-\u8a2d\u5b9a-Swift-300x201.png 300w, https:\/\/www.nickyoo7.com\/wp-content\/uploads\/2023\/10\/\u9060\u7aef\u63a8\u64ad-xcode-\u8a2d\u5b9a-Swift-768x515.png 768w, https:\/\/www.nickyoo7.com\/wp-content\/uploads\/2023\/10\/\u9060\u7aef\u63a8\u64ad-xcode-\u8a2d\u5b9a-Swift-1024x687.png 1024w, https:\/\/www.nickyoo7.com\/wp-content\/uploads\/2023\/10\/\u9060\u7aef\u63a8\u64ad-xcode-\u8a2d\u5b9a-Swift.png 1368w\" sizes=\"auto, (max-width: 1368px) 100vw, 1368px\" \/><br \/>\nStep 03 : \u81ea\u8a02 AppDelegate \u985e\u5225<br \/>\n\u5728 \u5c08\u6848\u540d+app.swift \u6a94\u7684 @main \u524d\u52a0\u5165<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"swift\">class AppDelegate: UIResponder, UIApplicationDelegate {\r\n    \r\n    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -&gt; Bool {\r\n        \/\/ Override point for customization after application launch.\r\n        \r\n        application.registerForRemoteNotifications() \/\/\u555f\u7528 Remote Notification\r\n        return true\r\n    }\r\n    func application(\r\n        _ application: UIApplication,\r\n        didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data\r\n    ){\r\n         let token = deviceToken.map { String(format: \"%02.2hhx\", $0) }.joined()\r\n         print(token)\r\n    }\r\n    func application(\r\n        _ application: UIApplication,\r\n        didFailToRegisterForRemoteNotificationsWithError error: Error\r\n    ){\r\n       \/\/\u7121\u6cd5\u63a8\u64ad\u7684\u932f\u8aa4\u8a0a\u606f\r\n       print(error)\r\n    }\r\n}<\/pre>\n<p>\u5728 \u5c08\u6848\u540d+app.swift \u6a94\u7684 struct \u5c08\u6848\u540d+App:\u00a0 App{ \u5167\u52a0\u5165<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"swift\">@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-256\" src=\"https:\/\/www.nickyoo7.com\/wp-content\/uploads\/2023\/10\/RemoteNotification-Swift.01.png\" alt=\"\" width=\"1368\" height=\"1000\" srcset=\"https:\/\/www.nickyoo7.com\/wp-content\/uploads\/2023\/10\/RemoteNotification-Swift.01-300x219.png 300w, https:\/\/www.nickyoo7.com\/wp-content\/uploads\/2023\/10\/RemoteNotification-Swift.01-768x561.png 768w, https:\/\/www.nickyoo7.com\/wp-content\/uploads\/2023\/10\/RemoteNotification-Swift.01-1024x749.png 1024w, https:\/\/www.nickyoo7.com\/wp-content\/uploads\/2023\/10\/RemoteNotification-Swift.01.png 1368w\" sizes=\"auto, (max-width: 1368px) 100vw, 1368px\" \/><\/p>\n<p>Step04 : \u5728 ContentView.swift import UserNotifications \u4e26\u5728\u555f\u59cb view \u52a0\u5165\u4e0b\u5217\u4ee3\u78bc<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"swift\">init(){\r\n    \/\/MARK: \u53d6\u5f97\u63a8\u64ad\u63f4\u6b0a\r\n    Task{\r\n        let opt:UNAuthorizationOptions = [.alert, .badge, .sound]\r\n        if try! await UNUserNotificationCenter.current().requestAuthorization(options: opt){\r\n            \r\n        } else {\r\n            \r\n        }\r\n    }\r\n}<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-257\" src=\"https:\/\/www.nickyoo7.com\/wp-content\/uploads\/2023\/10\/RemoteNotification-Swift.02.png\" alt=\"\" width=\"1430\" height=\"1000\" srcset=\"https:\/\/www.nickyoo7.com\/wp-content\/uploads\/2023\/10\/RemoteNotification-Swift.02-300x210.png 300w, https:\/\/www.nickyoo7.com\/wp-content\/uploads\/2023\/10\/RemoteNotification-Swift.02-768x537.png 768w, https:\/\/www.nickyoo7.com\/wp-content\/uploads\/2023\/10\/RemoteNotification-Swift.02-1024x716.png 1024w, https:\/\/www.nickyoo7.com\/wp-content\/uploads\/2023\/10\/RemoteNotification-Swift.02.png 1430w\" sizes=\"auto, (max-width: 1430px) 100vw, 1430px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>iOS \u5efa\u7acb\u9060\u8ddd\u63a8\u64ad App (\u4e00) Swift \u7a0b\u5f0f\u7bc7<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[],"class_list":["post-254","post","type-post","status-publish","format-standard","hentry","category-ios","entry"],"_links":{"self":[{"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=\/wp\/v2\/posts\/254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=254"}],"version-history":[{"count":2,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=\/wp\/v2\/posts\/254\/revisions"}],"predecessor-version":[{"id":262,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=\/wp\/v2\/posts\/254\/revisions\/262"}],"wp:attachment":[{"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}