{"id":7080,"date":"2025-06-01T14:54:57","date_gmt":"2025-06-05T07:13:56","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7080"},"modified":"2025-06-05T14:54:57","modified_gmt":"2025-06-05T07:13:56","slug":"https-badgameshow-com-steven-215","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-215\/","title":{"rendered":"\u5982\u4f55\u5728 Swift \u4e2d\u8a2d\u7f6e\u63a8\u64ad\u901a\u77e5\uff1a2025 \u6700\u65b0\u6559\u5b78"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, \u63a8\u64ad\u901a\u77e5, iOS\u63a8\u64ad\u901a\u77e5, Swift\u8a2d\u7f6e\u63a8\u64ad\u901a\u77e5\"><\/p>\n<h1>\u5982\u4f55\u5728 Swift \u4e2d\u8a2d\u7f6e\u63a8\u64ad\u901a\u77e5\uff1a2025 \u6700\u65b0\u6559\u5b78<\/h1>\n<p>\u5728 iOS \u958b\u767c\u4e2d\uff0c\u63a8\u64ad\u901a\u77e5\u662f\u8207\u7528\u6236\u4fdd\u6301\u806f\u7e6b\u7684\u91cd\u8981\u529f\u80fd\u3002\u900f\u904e\u63a8\u64ad\u901a\u77e5\uff0c\u61c9\u7528\u7a0b\u5f0f\u53ef\u4ee5\u5be6\u6642\u901a\u77e5\u7528\u6236\u91cd\u8981\u7684\u66f4\u65b0\u6216\u63d0\u9192\u3002\u5728\u672c\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u6df1\u5165\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u8a2d\u7f6e\u63a8\u64ad\u901a\u77e5\uff0c\u4e26\u5305\u542b\u6700\u65b0\u7684\u8a9e\u6cd5\u53ca\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u524d\u7f6e\u6e96\u5099<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u78ba\u4fdd\u60a8\u7684\u61c9\u7528\u7a0b\u5f0f\u5df2\u7d93\u8a3b\u518a\u63a8\u64ad\u901a\u77e5\u3002\u60a8\u9700\u8981\u5728 Apple Developer \u6703\u54e1\u5e33\u6236\u4e2d\u914d\u7f6e\u63a8\u64ad\u901a\u77e5\uff0c\u4e26\u78ba\u4fdd\u61c9\u7528\u7a0b\u5f0f\u7684 Info.plist \u4e2d\u5305\u542b\u6240\u9700\u7684\u6b0a\u9650\u8a2d\u7f6e\u3002<\/p>\n<h2>\u8a2d\u7f6e\u63a8\u64ad\u901a\u77e5<\/h2>\n<p>\u4ee5\u4e0b\u662f\u8a2d\u7f6e\u63a8\u64ad\u901a\u77e5\u7684\u6b65\u9a5f\uff1a<\/p>\n<ol>\n<li>\u5c0e\u5165 <code>UserNotifications<\/code> \u6846\u67b6\uff1a<\/li>\n<pre><code class=\"language-swift line-numbers\">import UserNotifications<\/code><\/pre>\n<li>\u8acb\u6c42\u7528\u6236\u5141\u8a31\u63a5\u6536\u63a8\u64ad\u901a\u77e5\uff1a<\/li>\n<pre><code class=\"language-swift line-numbers\">UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in\n        if let error = error {\n            \/\/ \u8655\u7406\u932f\u8aa4\n            print(\"Error requesting authorization: \\(error)\")\n        }\n        \/\/ \u9a57\u8b49\u7528\u6236\u662f\u5426\u6388\u6b0a\n        print(\"Notification permission granted: \\(granted)\")\n    }<\/code><\/pre>\n<li>\u5275\u5efa\u63a8\u64ad\u901a\u77e5\u5167\u5bb9\uff1a<\/li>\n<pre><code class=\"language-swift line-numbers\">let content = UNMutableNotificationContent()\ncontent.title = \"\u63a8\u64ad\u901a\u77e5\"\ncontent.body = \"\u9019\u662f\u4e00\u500b\u63a8\u64ad\u901a\u77e5\u7684\u7bc4\u4f8b\"\ncontent.sound = UNNotificationSound.default<\/code><\/pre>\n<li>\u8a2d\u7f6e\u89f8\u767c\u689d\u4ef6\uff1a<\/li>\n<pre><code class=\"language-swift line-numbers\">let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)<\/code><\/pre>\n<li>\u5275\u5efa\u901a\u77e5\u8acb\u6c42\uff1a<\/li>\n<pre><code class=\"language-swift line-numbers\">let request = UNNotificationRequest(identifier: \"notification\", content: content, trigger: trigger)<\/code><\/pre>\n<li>\u5c07\u8acb\u6c42\u6dfb\u52a0\u5230\u901a\u77e5\u4e2d\u5fc3\uff1a<\/li>\n<pre><code class=\"language-swift line-numbers\">UNUserNotificationCenter.current().add(request) { (error) in\n        if let error = error {\n            \/\/ \u5982\u679c\u6709\u932f\u8aa4\uff0c\u5247\u5728\u6b64\u8655\u9032\u884c\u8655\u7406\n            print(\"Error adding notification: \\(error)\")\n        }\n    }<\/code><\/pre>\n<\/ol>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5982\u679c\u60a8\u5728\u8a2d\u7f6e\u63a8\u64ad\u901a\u77e5\u6642\u9047\u5230\u554f\u984c\uff0c\u8acb\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u78ba\u4fdd\u61c9\u7528\u7a0b\u5f0f\u5177\u6709\u9069\u7576\u7684\u6b0a\u9650\u3002<\/li>\n<li>\u6aa2\u67e5\u63a8\u64ad\u901a\u77e5\u7684\u914d\u7f6e\u662f\u5426\u6b63\u78ba\u3002<\/li>\n<li>\u5728\u6a21\u64ec\u5668\u4e0a\u6e2c\u8a66\u63a8\u64ad\u901a\u77e5\u6642\uff0c\u8acb\u6ce8\u610f\u6a21\u64ec\u5668\u4e0d\u652f\u6301\u6b64\u529f\u80fd\uff0c\u61c9\u4f7f\u7528\u771f\u5be6\u8a2d\u5099\u6e2c\u8a66\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u63a8\u64ad\u901a\u77e5\u53ef\u4ee5\u9032\u4e00\u6b65\u64f4\u5c55\u5230\u66f4\u591a\u529f\u80fd\uff0c\u4f8b\u5982\u6dfb\u52a0\u52d5\u4f5c\u6309\u9215\u6216\u4f7f\u7528\u7fa4\u7d44\u901a\u77e5\u4f86\u7d44\u7e54\u591a\u500b\u901a\u77e5\u3002\u60a8\u53ef\u4ee5\u63a2\u7d22 <code>UNNotificationCategory<\/code> \u4f86\u8a2d\u7f6e\u81ea\u5b9a\u7fa9\u64cd\u4f5c\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u8a2d\u7f6e\u63a8\u64ad\u901a\u77e5-\ud83d\udd14_1675402008.5946088.webp\" alt=\"Swift \u8a2d\u7f6e\u63a8\u64ad\u901a\u77e5 \ud83d\udd14\" title=\"Swift\u63a8\u64ad\u901a\u77e5, Swift\u8a2d\u7f6e\u63a8\u64ad, Swift\u63a8\u64ad\u8a2d\u5b9a, Swift\u63a8\u64ad\u529f\u80fd, Swift\u63a8\u64ad\u65b9\u6cd5\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. \u5982\u4f55\u6aa2\u67e5\u7528\u6236\u662f\u5426\u6388\u6b0a\u63a8\u64ad\u901a\u77e5\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>UNUserNotificationCenter.current().getNotificationSettings<\/code> \u65b9\u6cd5\u4f86\u6aa2\u67e5\u7576\u524d\u7684\u901a\u77e5\u8a2d\u5b9a\u3002<\/p>\n<h3>2. \u5982\u4f55\u8a2d\u7f6e\u91cd\u8907\u7684\u63a8\u64ad\u901a\u77e5\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u5c07 <code>UNTimeIntervalNotificationTrigger<\/code> \u7684 <code>repeats<\/code> \u53c3\u6578\u8a2d\u7f6e\u70ba <code>true<\/code>\uff0c\u9019\u6a23\u53ef\u4ee5\u6bcf\u9694\u4e00\u6bb5\u6642\u9593\u91cd\u8907\u767c\u9001\u901a\u77e5\u3002<\/p>\n<h3>3. \u53ef\u4ee5\u5728\u63a8\u64ad\u901a\u77e5\u4e2d\u6dfb\u52a0\u5716\u7247\u55ce\uff1f<\/h3>\n<p>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>UNNotificationAttachment<\/code> \u4f86\u6dfb\u52a0\u5716\u7247\u6216\u5176\u4ed6\u5a92\u9ad4\u5230\u901a\u77e5\u4e2d\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:\u5728Swift\u4e2d\u8a2d\u7f6e\u63a8\u64ad\u901a\u77e5\u662f\u4e00\u500b\u5f88\u5bb9\u6613\u7684\u4e8b\u60c5\uff0c\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swift\u7684methods()\u51fd\u6578\u4f86\u8a2d\u7f6e\u63a8\u64ad\u901a\u77e5\uff0c\u8b93\u4f60\u53ef\u4ee5\u8f15\u9b06\u5730\u638c\u63e1\u63a8\u64ad\u901a\u77e5\u7684\u8a2d\u7f6e\u65b9\u6cd5\u3002<\/p>\n","protected":false},"author":1,"featured_media":7078,"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-7080","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-\u8a2d\u7f6e\u63a8\u64ad\u901a\u77e5-\ud83d\udd14_1675402008.5946088.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Qc","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7080","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=7080"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7080\/revisions"}],"predecessor-version":[{"id":13216,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7080\/revisions\/13216"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7078"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}