{"id":4029,"date":"2025-05-31T11:29:32","date_gmt":"2025-06-05T03:59:31","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4029"},"modified":"2025-06-05T11:29:32","modified_gmt":"2025-06-05T03:59:31","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8swift%e4%b8%ad%e7%9a%84%e8%ae%a1%e6%ad%a5%e5%99%a8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8swift%e4%b8%ad%e7%9a%84%e8%ae%a1%e6%ad%a5%e5%99%a8\/","title":{"rendered":"\u5168\u9762\u638c\u63e1 Swift \u4e2d\u7684 CMStepCounter \u8ba1\u6b65\u5668\u4f7f\u7528\u6280\u5de7\uff082025 \u6700\u65b0\u7248\uff09"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"Swift, CMStepCounter, \u8ba1\u6b65\u5668, \u6570\u636e, \u76d1\u542c, \u5904\u7406, iOS, \u5065\u5eb7\u5e94\u7528\"><\/p>\n<h1>\u5168\u9762\u638c\u63e1 Swift \u4e2d\u7684 CMStepCounter \u8ba1\u6b65\u5668\u4f7f\u7528\u6280\u5de7\uff082025 \u6700\u65b0\u7248\uff09<\/h1>\n<p>\u5728\u73b0\u4ee3\u79fb\u52a8\u5e94\u7528\u4e2d\uff0c\u5065\u5eb7\u8ddf\u8e2a\u529f\u80fd\u65e5\u76ca\u91cd\u8981\u3002Swift \u63d0\u4f9b\u4e86 <strong>CMStepCounter<\/strong> \u7c7b\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u83b7\u53d6\u548c\u5904\u7406\u8ba1\u6b65\u5668\u6570\u636e\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 <strong>CMStepCounter<\/strong> \u7c7b\u6765\u83b7\u53d6\u6b65\u6570\u6570\u636e\u3001\u5b9e\u65f6\u76d1\u542c\u6570\u636e\u53d8\u5316\u4ee5\u53ca\u5982\u4f55\u5904\u7406\u8fd9\u4e9b\u6570\u636e\uff0c\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u8fdb\u884c\u5e94\u7528\u5f00\u53d1\u3002<\/p>\n<h2>\u83b7\u53d6\u8ba1\u6b65\u5668\u6570\u636e<\/h2>\n<p>\u8981\u83b7\u53d6\u8ba1\u6b65\u5668\u6570\u636e\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2a <strong>CMStepCounter<\/strong> \u5b9e\u4f8b\uff0c\u5e76\u8c03\u7528\u5b83\u7684 <strong>queryStepCountStarting(from:to:to:withHandler:)<\/strong> \u65b9\u6cd5\uff1a<\/p>\n<pre class=\"brush: swift\">\nimport CoreMotion\n\nlet stepCounter = CMStepCounter()\nstepCounter.queryStepCountStarting(from: Date(), to: Date(), to: .main) { (steps, error) in\n    if let error = error {\n        print(\"Error: \\(error.localizedDescription)\")\n    } else {\n        print(\"Steps: \\(steps)\")\n    }\n}\n<\/pre>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u8fd4\u56de\u4e00\u4e2a <strong>steps<\/strong> \u53c2\u6570\uff0c\u5b83\u662f\u4e00\u4e2a <strong>Int<\/strong> \u7c7b\u578b\u7684\u503c\uff0c\u8868\u793a\u4ece\u6307\u5b9a\u65f6\u95f4\u5f00\u59cb\u5230\u5f53\u524d\u65f6\u95f4\u7684\u6b65\u6570\u3002<\/p>\n<h2>\u76d1\u542c\u8ba1\u6b65\u5668\u6570\u636e\u53d8\u5316<\/h2>\n<p>\u5982\u679c\u4f60\u60f3\u8981\u5b9e\u65f6\u76d1\u542c\u8ba1\u6b65\u5668\u6570\u636e\u7684\u53d8\u5316\uff0c\u53ef\u4ee5\u4f7f\u7528 <strong>startStepCountingUpdates(to:withHandler:)<\/strong> \u65b9\u6cd5\uff1a<\/p>\n<pre class=\"brush: swift\">\nlet stepCounter = CMStepCounter()\nstepCounter.startStepCountingUpdates(to: .main) { (steps, error) in\n    if let error = error {\n        print(\"Error: \\(error.localizedDescription)\")\n    } else {\n        print(\"Current Steps: \\(steps)\")\n    }\n}\n<\/pre>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u6bcf\u9694\u4e00\u6bb5\u65f6\u95f4\u8c03\u7528\u4e00\u6b21\u56de\u8c03\u51fd\u6570\uff0c\u5e76\u8fd4\u56de\u5f53\u524d\u7684\u6b65\u6570\u3002\u8fd9\u4f7f\u5f97\u5e94\u7528\u80fd\u591f\u5b9e\u65f6\u66f4\u65b0\u7528\u6237\u7684\u6b65\u6570\u3002<\/p>\n<h2>\u5904\u7406\u8ba1\u6b65\u5668\u6570\u636e<\/h2>\n<p>\u5f53\u4f60\u83b7\u53d6\u5230\u8ba1\u6b65\u5668\u6570\u636e\u540e\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u8ba1\u7b97\u51fa\u6bcf\u5929\u7684\u6b65\u6570\uff0c\u6216\u8005\u8ba1\u7b97\u51fa\u6bcf\u5468\u7684\u6b65\u6570\uff1a<\/p>\n<pre class=\"brush: swift\">\n\/\/ \u5047\u8bbe steps \u662f\u4e00\u4e2a\u5305\u542b\u65e5\u671f\u548c\u6b65\u6570\u7684\u6570\u7ec4\nlet dailySteps = steps.reduce(into: [Date: Int]()) { (result, step) in\n    let date = Calendar.current.startOfDay(for: step.date)\n    result[date, default: 0] += step.value\n}\n\n\/\/ \u8ba1\u7b97\u6bcf\u5468\u7684\u6b65\u6570\nlet weeklySteps = steps.reduce(into: [Date: Int]()) { (result, step) in\n    let date = Calendar.current.startOfWeek(for: step.date)\n    result[date, default: 0] += step.value\n}\n<\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u5904\u7406\uff0c\u4f60\u53ef\u4ee5\u66f4\u6df1\u5165\u5730\u4e86\u89e3\u7528\u6237\u7684\u5065\u5eb7\u72b6\u51b5\uff0c\u5e76\u63d0\u4f9b\u4e2a\u6027\u5316\u7684\u5065\u5eb7\u5efa\u8bae\u3002<\/p>\n<h2>\u7ed3\u8bba<\/h2>\n<p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528 <strong>CMStepCounter<\/strong> \u7c7b\u6765\u83b7\u53d6\u8ba1\u6b65\u5668\u6570\u636e\u3001\u76d1\u542c\u6570\u636e\u53d8\u5316\u4ee5\u53ca\u5904\u7406\u6570\u636e\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\uff0c\u4f60\u53ef\u4ee5\u5f00\u53d1\u51fa\u529f\u80fd\u4e30\u5bcc\u7684\u5065\u5eb7\u5e94\u7528\uff0c\u5e2e\u52a9\u7528\u6237\u66f4\u597d\u5730\u7ba1\u7406\u4ed6\u4eec\u7684\u5065\u5eb7\u3002<\/p>\n<p>\u5982\u9700\u8fdb\u4e00\u6b65\u4e86\u89e3 <strong>CMStepCounter<\/strong> \u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u8bf7\u53c2\u8003\u5b98\u65b9\u6587\u6863\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u89c1\u95ee\u9898\u89e3\u7b54\uff09<\/h2>\n<h3>1. \u5982\u4f55\u5728 Swift \u4e2d\u68c0\u67e5\u8bbe\u5907\u662f\u5426\u652f\u6301\u8ba1\u6b65\u5668\uff1f<\/h3>\n<p>\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5 <strong>CMStepCounter.isStepCountingAvailable()<\/strong> \u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u6765\u5224\u65ad\u8bbe\u5907\u662f\u5426\u652f\u6301\u8ba1\u6b65\u5668\u3002<\/p>\n<h3>2. \u4e3a\u4ec0\u4e48\u6211\u83b7\u53d6\u7684\u6b65\u6570\u6570\u636e\u6709\u65f6\u4e3a\u96f6\uff1f<\/h3>\n<p>\u8fd9\u53ef\u80fd\u662f\u56e0\u4e3a\u8ba1\u6b65\u5668\u5728\u6307\u5b9a\u65f6\u95f4\u6bb5\u5185\u6ca1\u6709\u8bb0\u5f55\u5230\u4efb\u4f55\u6b65\u6570\uff0c\u6216\u8005\u8bbe\u5907\u7684\u8ba1\u6b65\u5668\u529f\u80fd\u672a\u5f00\u542f\u3002\u8bf7\u786e\u4fdd\u8bbe\u5907\u5728\u79fb\u52a8\u72b6\u6001\u5e76\u53ef\u4ee5\u6b63\u5e38\u4f7f\u7528\u8ba1\u6b65\u5668\u529f\u80fd\u3002<\/p>\n<h3>3. \u5982\u4f55\u5728\u5e94\u7528\u4e2d\u663e\u793a\u6b65\u6570\u6570\u636e\uff1f<\/h3>\n<p>\u53ef\u4ee5\u901a\u8fc7\u5c06\u83b7\u53d6\u5230\u7684\u6b65\u6570\u6570\u636e\u7ed1\u5b9a\u5230\u7528\u6237\u754c\u9762\u5143\u7d20\uff08\u5982\u6807\u7b7e\u6216\u56fe\u8868\uff09\u6765\u5c55\u793a\u6b65\u6570\u6570\u636e\uff0c\u4ee5\u4fbf\u7528\u6237\u5b9e\u65f6\u67e5\u770b\u4ed6\u4eec\u7684\u6d3b\u52a8\u60c5\u51b5\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swift\u4e2d\u7684\u8ba1\u6b65\u5668\uff0c\u5305\u62ec\u5982\u4f55\u4f7f\u7528CMStepCounter\u7c7b\u83b7\u53d6\u8ba1\u6b65\u5668\u6570\u636e\u3001\u5982\u4f55\u76d1\u542c\u8ba1\u6b65\u5668\u7684\u6570\u636e\u53d8\u5316\u3001\u5982\u4f55\u5904\u7406\u8ba1\u6b65\u5668\u7684\u6570\u636e\u7b49\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","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-4029","post","type-post","status-publish","format-standard","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-12Z","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4029","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=4029"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4029\/revisions"}],"predecessor-version":[{"id":4030,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4029\/revisions\/4030"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}