{"id":6834,"date":"2025-05-30T13:53:29","date_gmt":"2025-06-05T05:56:29","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6834"},"modified":"2025-06-05T13:53:29","modified_gmt":"2025-06-05T05:56:29","slug":"https-badgameshow-com-steven-93","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-93\/","title":{"rendered":"2025 \u6700\u65b0 Swift \u986f\u793a\u901a\u77e5\u6559\u5b78\uff1a\u63a8\u9001\u901a\u77e5\u7684\u5b8c\u6574\u6d41\u7a0b"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, \u986f\u793a\u901a\u77e5, \u63a8\u9001\u901a\u77e5, iOS \u901a\u77e5\u7ba1\u7406\"><\/p>\n<h2>\u5f15\u8a00<\/h2>\n<p>Swift \u986f\u793a\u901a\u77e5 \ud83d\udd14 \u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u8b93\u60a8\u5728\u61c9\u7528\u7a0b\u5f0f\u4e2d\u63a8\u9001\u901a\u77e5\uff0c\u8b93\u7528\u6236\u96a8\u6642\u63a5\u6536\u66f4\u65b0\u6216\u91cd\u8981\u8a0a\u606f\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u5275\u5efa\u548c\u7ba1\u7406\u986f\u793a\u901a\u77e5\u7684\u529f\u80fd\uff0c\u4e26\u63d0\u4f9b\u5b8c\u6574\u7684\u6559\u5b78\u6d41\u7a0b\u53ca\u5be6\u4f5c\u7bc4\u4f8b\u3002<\/p>\n<h2>\u6e96\u5099\u5de5\u4f5c<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u8acb\u78ba\u4fdd\u60a8\u5df2\u7d93\u5728 Xcode \u4e2d\u5275\u5efa\u4e86\u4e00\u500b\u65b0\u7684 iOS \u5c08\u6848\u3002\u63a5\u4e0b\u4f86\uff0c\u60a8\u9700\u8981\u5728\u5c08\u6848\u4e2d\u555f\u7528\u901a\u77e5\u529f\u80fd\u3002\u8acb\u524d\u5f80 <strong>Signing &#038; Capabilities<\/strong> \u6a19\u7c64\uff0c\u7136\u5f8c\u555f\u7528 <strong>Push Notifications<\/strong> \u548c <strong>Background Modes<\/strong> \u4e2d\u7684 <strong>Remote notifications<\/strong>\u3002<\/p>\n<h2>\u5c0e\u5165\u5fc5\u8981\u7684\u6a21\u7d44<\/h2>\n<p>\u60a8\u9700\u8981\u5728\u60a8\u7684 Swift \u6a94\u6848\u4e2d\u5c0e\u5165 <strong>UserNotifications<\/strong> \u6a21\u7d44\uff0c\u4ee5\u4fbf\u4f7f\u7528\u901a\u77e5\u76f8\u95dc\u7684\u529f\u80fd\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">import UserNotifications<\/code><\/pre>\n<h2>\u8acb\u6c42\u4f7f\u7528\u8005\u6388\u6b0a<\/h2>\n<p>\u5728\u986f\u793a\u901a\u77e5\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u8acb\u6c42\u4f7f\u7528\u8005\u7684\u6388\u6b0a\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in\n    if granted {\n        print(\"\u6388\u6b0a\u6210\u529f\")\n    } else {\n        print(\"\u6388\u6b0a\u5931\u6557\uff1a\\(error?.localizedDescription ?? \"\u672a\u77e5\u932f\u8aa4\")\")\n    }\n}<\/code><\/pre>\n<h2>\u5275\u5efa\u901a\u77e5\u5167\u5bb9<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u60a8\u9700\u8981\u5275\u5efa\u4e00\u500b <strong>UNMutableNotificationContent<\/strong> \u5c0d\u8c61\uff0c\u5b83\u5c07\u5305\u542b\u60a8\u8981\u986f\u793a\u7684\u6240\u6709\u5167\u5bb9\uff0c\u4f8b\u5982\u6a19\u984c\u3001\u5167\u5bb9\u53ca\u8072\u97f3\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let content = UNMutableNotificationContent()\ncontent.title = \"\u901a\u77e5\u6a19\u984c\"\ncontent.body = \"\u9019\u662f\u4e00\u689d\u901a\u77e5\u5167\u5bb9\"\ncontent.sound = UNNotificationSound.default\n<\/code><\/pre>\n<h2>\u8a2d\u5b9a\u901a\u77e5\u89f8\u767c\u689d\u4ef6<\/h2>\n<p>\u5275\u5efa\u4e00\u500b <strong>UNNotificationRequest<\/strong> \u5c0d\u8c61\uff0c\u60a8\u53ef\u4ee5\u8a2d\u5b9a\u901a\u77e5\u7684\u89f8\u767c\u6642\u9593\u3002\u4ee5\u4e0b\u7bc4\u4f8b\u662f\u5728\u4e94\u79d2\u5f8c\u986f\u793a\u901a\u77e5\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)\nlet request = UNNotificationRequest(identifier: \"notification\", content: content, trigger: trigger)<\/code><\/pre>\n<h2>\u6dfb\u52a0\u901a\u77e5\u8acb\u6c42\u5230\u901a\u77e5\u4e2d\u5fc3<\/h2>\n<p>\u6700\u5f8c\uff0c\u60a8\u9700\u8981\u5c07 <strong>UNNotificationRequest<\/strong> \u5c0d\u8c61\u6dfb\u52a0\u5230 <strong>UNUserNotificationCenter<\/strong> \u4e2d\uff0c\u4ee5\u4fbf\u7528\u6236\u53ef\u4ee5\u6536\u5230\u901a\u77e5\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">UNUserNotificationCenter.current().add(request) { (error) in\n    if let error = error {\n        print(\"\u6dfb\u52a0\u901a\u77e5\u5931\u6557\uff1a\\(error.localizedDescription)\")\n    }\n}<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5982\u679c\u901a\u77e5\u7121\u6cd5\u986f\u793a\uff0c\u8acb\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u78ba\u4fdd\u4f7f\u7528\u8005\u5df2\u6388\u6b0a\u63a5\u6536\u901a\u77e5\u3002<\/li>\n<li>\u6aa2\u67e5\u901a\u77e5\u662f\u5426\u6b63\u78ba\u8a2d\u7f6e\u3002<\/li>\n<li>\u78ba\u8a8d\u61c9\u7528\u7a0b\u5f0f\u662f\u5426\u5728\u524d\u666f\u6216\u80cc\u666f\u4e2d\u904b\u884c\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u901a\u77e5\uff0c\u60a8\u9084\u53ef\u4ee5\u8003\u616e\u4f7f\u7528 <strong>UNNotificationCategory<\/strong> \u4f86\u5275\u5efa\u4e92\u52d5\u5f0f\u901a\u77e5\uff0c\u8b93\u7528\u6236\u53ef\u4ee5\u76f4\u63a5\u5728\u901a\u77e5\u4e2d\u57f7\u884c\u67d0\u4e9b\u52d5\u4f5c\uff0c\u4f8b\u5982\u56de\u8986\u8a0a\u606f\u6216\u6a19\u8a18\u70ba\u5df2\u8b80\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u986f\u793a\u901a\u77e5-\ud83d\udd14_1675396646.4380102.webp\" alt=\"Swift \u986f\u793a\u901a\u77e5 \ud83d\udd14\" title=\"Swift \u986f\u793a\u901a\u77e5, Swift \u65b9\u6cd5, Swift \u51fd\u6578, iOS \u986f\u793a\u901a\u77e5, iOS \u7ba1\u7406\u901a\u77e5\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. \u5982\u4f55\u5728 Swift \u4e2d\u6e2c\u8a66\u901a\u77e5\u529f\u80fd\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u5728 iOS \u6a21\u64ec\u5668\u4e2d\u76f4\u63a5\u904b\u884c\u61c9\u7528\u7a0b\u5f0f\uff0c\u4e26\u67e5\u770b\u662f\u5426\u80fd\u5728\u6307\u5b9a\u7684\u6642\u9593\u5167\u6536\u5230\u901a\u77e5\u3002<\/p>\n<h3>2. \u63a8\u9001\u901a\u77e5\u8207\u672c\u5730\u901a\u77e5\u6709\u4ec0\u9ebc\u4e0d\u540c\uff1f<\/h3>\n<p>\u672c\u5730\u901a\u77e5\u662f\u7531\u61c9\u7528\u7a0b\u5f0f\u672c\u8eab\u767c\u9001\u7684\u901a\u77e5\uff0c\u800c\u63a8\u9001\u901a\u77e5\u9700\u8981\u900f\u904e\u4f3a\u670d\u5668\u4f86\u767c\u9001\u7d66\u7528\u6236\u7684\u8a2d\u5099\u3002<\/p>\n<h3>3. \u5982\u4f55\u7ba1\u7406\u5df2\u986f\u793a\u7684\u901a\u77e5\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 <strong>UNUserNotificationCenter<\/strong> \u63d0\u4f9b\u7684\u529f\u80fd\u4f86\u67e5\u8a62\u6216\u522a\u9664\u5df2\u986f\u793a\u7684\u901a\u77e5\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Swift\u4e2d\u7684\u986f\u793a\u901a\u77e5\ud83d\udd14\uff0c\u4e86\u89e3\u5982\u4f55\u4f7f\u7528Swift\u4e2d\u7684\u65b9\u6cd5\u548c\u51fd\u6578\u4f86\u8a2d\u7f6e\u986f\u793a\u901a\u77e5\uff0c\u4ee5\u53ca\u5982\u4f55\u5728iOS\u4e0a\u5efa\u7acb\u548c\u7ba1\u7406\u986f\u793a\u901a\u77e5\u3002<\/p>\n","protected":false},"author":1,"featured_media":6833,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[174,5],"tags":[173,9],"class_list":["post-6834","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u986f\u793a\u901a\u77e5-\ud83d\udd14_1675396646.4380102.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Me","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6834","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=6834"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6834\/revisions"}],"predecessor-version":[{"id":13512,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6834\/revisions\/13512"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6833"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}