{"id":1659,"date":"2022-06-09T00:00:57","date_gmt":"2022-06-08T15:00:57","guid":{"rendered":"https:\/\/www.typea.info\/blog\/?p=1659"},"modified":"2022-06-09T00:01:40","modified_gmt":"2022-06-08T15:01:40","slug":"swift_watchos_save_healthdata","status":"publish","type":"post","link":"https:\/\/www.typea.info\/blog\/index.php\/2022\/06\/09\/swift_watchos_save_healthdata\/","title":{"rendered":"Swift watchOS \u3067 \u30d8\u30eb\u30b9\u30c7\u30fc\u30bf\u3092\u767b\u9332\u3059\u308b"},"content":{"rendered":"<h2><a href=\"https:\/\/developer.apple.com\/documentation\/healthkit\/saving_data_to_healthkit\">1.\u30d8\u30eb\u30b9\u30c7\u30fc\u30bf\u3092healthkit\u306b\u4fdd\u5b58\u3059\u308b<\/a><\/h2>\n<p>\u30a2\u30d7\u30ea\u306f\u65b0\u3057\u3044\u30b5\u30f3\u30d7\u30eb\u3092\u4f5c\u6210\u3057\u3001HealthKit\u30b9\u30c8\u30a2\u306b\u8ffd\u52a0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3059\u3079\u3066\u306e\u30b5\u30f3\u30d7\u30eb\u30bf\u30a4\u30d7\u306f\u540c\u69d8\u306e\u624b\u9806\u3092\u8e0f\u307f\u307e\u3059\u304c\u3001\u5404\u30bf\u30a4\u30d7\u306b\u306f\u72ec\u81ea\u306e\u30d0\u30ea\u30a8\u30fc\u30b7\u30e7\u30f3\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<ol>\n<li>\u30c7\u30fc\u30bf\u306e\u578b\u5f0f\u8b58\u5225\u5b50\u3092\u8abf\u3079\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u30e6\u30fc\u30b6\u30fc\u306e\u4f53\u91cd\u3092\u8a18\u9332\u3059\u308b\u306b\u306f\u3001bodyMass \u578b\u306e\u8b58\u5225\u5b50\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u578b\u8b58\u5225\u5b50\u306e\u5b8c\u5168\u306a\u30ea\u30b9\u30c8\u306b\u3064\u3044\u3066\u306f\u3001\u300c<a href=\"https:\/\/developer.apple.com\/documentation\/healthkit\/data_types\">\u30c7\u30fc\u30bf\u578b<\/a>\u300d\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/li>\n<li><a href=\"https:\/\/developer.apple.com\/documentation\/healthkit\/hkobjecttype\">HKObjectType<\/a>\u30af\u30e9\u30b9\u306e\u4fbf\u5229\u306a\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u3001\u30c7\u30fc\u30bf\u306b\u9069\u3057\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8 \u30bf\u30a4\u30d7\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<br \/>\n\u305f\u3068\u3048\u3070\u3001\u30e6\u30fc\u30b6\u30fc\u306e\u4f53\u91cd\u3092\u4fdd\u5b58\u3059\u308b\u306b\u306f\u3001<a href=\"https:\/\/developer.apple.com\/documentation\/healthkit\/hkobjecttype\/1615298-quantitytype\">quantityType(forIdentifier:)<\/a>\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066<a href=\"https:\/\/developer.apple.com\/documentation\/healthkit\/hkquantitytype\">HKQuantityType<\/a>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3059\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n\u4fbf\u5229\u306a\u30e1\u30bd\u30c3\u30c9\u306e\u4e00\u89a7\u306f\u3001<a href=\"https:\/\/developer.apple.com\/documentation\/healthkit\/hkobjecttype\">HKObjectType<\/a>\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/li>\n<li>\u4e00\u81f4\u3059\u308b <a href=\"https:\/\/developer.apple.com\/documentation\/healthkit\/hksample\">HKSample<\/a> \u30b5\u30d6\u30af\u30e9\u30b9\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u30fb\u30bf\u30a4\u30d7\u3092\u4f7f\u7528\u3057\u3066\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316\u3057\u307e\u3059\u3002<\/li>\n<li><a href=\"https:\/\/developer.apple.com\/documentation\/healthkit\/hkhealthstore\/1614168-save\">save(_:withCompletion:)<\/a> \u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u3001HealthKit \u30b9\u30c8\u30a2\u306b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4fdd\u5b58\u3057\u307e\u3059\u3002<\/li>\n<\/ol>\n<p>\u5404HKSample\u30b5\u30d6\u30af\u30e9\u30b9\u306f\u3001\u30b5\u30f3\u30d7\u30eb\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316\u3059\u308b\u305f\u3081\u306e\u72ec\u81ea\u306e\u4fbf\u5229\u306a\u30e1\u30bd\u30c3\u30c9\u3092\u6301\u3063\u3066\u304a\u308a\u3001\u4e0a\u8a18\u306e\u30ea\u30b9\u30c8\u3067\u8aac\u660e\u3057\u305f\u624b\u9806\u3092\u5909\u66f4\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<h3>1.2 \u6570\u91cf\u30b5\u30f3\u30d7\u30eb\u306e\u5834\u5408<\/h3>\n<p>HKQuantity\u30af\u30e9\u30b9\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002\u6570\u91cf\u306e\u5358\u4f4d\u306f\u3001\u578b\u8b58\u5225\u5b50\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306b\u8a18\u8ff0\u3055\u308c\u3066\u3044\u308b\u8a31\u5bb9\u5358\u4f4d\u306b\u5bfe\u5fdc\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u3001\u9ad8\u3055\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3067\u306f\u3001\u9577\u3055\u306e\u5358\u4f4d\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u304c\u8a18\u8f09\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u6570\u91cf\u306f\u30bb\u30f3\u30c1\u30e1\u30fc\u30c8\u30eb\u3001\u30e1\u30fc\u30c8\u30eb\u3001\u30d5\u30a3\u30fc\u30c8\u3001\u30a4\u30f3\u30c1\u3001\u307e\u305f\u306f\u305d\u306e\u4ed6\u306e\u4e92\u63db\u6027\u306e\u3042\u308b\u5358\u4f4d\u3092\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u8a73\u7d30\u306b\u3064\u3044\u3066\u306f\u3001\u300c<a href=\"https:\/\/developer.apple.com\/documentation\/healthkit\/hkquantitysample\">HKQuantitySample<\/a>\u300d\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h3>1.3 \u30ab\u30c6\u30b4\u30ea\u30b5\u30f3\u30d7\u30eb\u306e\u5834\u5408<\/h3>\n<p>\u30b5\u30f3\u30d7\u30eb\u306e\u5024\u306f\u3001\u578b\u8b58\u5225\u5b50\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306b\u8a18\u8f09\u3055\u308c\u3066\u3044\u308benum\u306b\u5bfe\u5fdc\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u305f\u3068\u3048\u3070\u3001sleepAnalysis\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3067\u306f\u3001HKCategoryValueSleepAnalysis\u306eenum\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u304c\u8a18\u8f09\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u3001\u3053\u306e\u30b5\u30f3\u30d7\u30eb\u3092\u4f5c\u6210\u3059\u308b\u3068\u304d\u306f\u3001\u3053\u306eenum\u304b\u3089\u5024\u3092\u6e21\u3059\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u8a73\u7d30\u306b\u3064\u3044\u3066\u306f\u3001\u300c<a href=\"https:\/\/developer.apple.com\/documentation\/healthkit\/hkcategorysample\">HKCategorySample<\/a>\u300d\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h3>1.4 \u76f8\u95a2\u95a2\u4fc2<\/h3>\n<p>\u307e\u305a\u3001\u76f8\u95a2\u95a2\u4fc2\u306b\u542b\u307e\u308c\u308b\u3059\u3079\u3066\u306e\u30b5\u30f3\u30d7\u30eb\u30fb\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u76f8\u95a2\u306e\u30bf\u30a4\u30d7\u8b58\u5225\u5b50\u306b\u306f\u3001\u542b\u307e\u308c\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30bf\u30a4\u30d7\u3068\u6570\u306e\u4e21\u65b9\u304c\u8a18\u8ff0\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u542b\u307e\u308c\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u3001HealthKit \u30b9\u30c8\u30a2\u306b\u4fdd\u5b58\u3057\u306a\u3044\u3067\u304f\u3060\u3055\u3044\u3002\u305d\u308c\u3089\u306f\u76f8\u95a2\u306e\u4e00\u90e8\u3068\u3057\u3066\u4fdd\u5b58\u3055\u308c\u307e\u3059\u3002\u8a73\u7d30\u306b\u3064\u3044\u3066\u306f\u3001\u300c<a href=\"https:\/\/developer.apple.com\/documentation\/healthkit\/hkcorrelation\">HKCorrelation<\/a>\u300d\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h2>2.\u30bd\u30fc\u30b9<\/h2>\n<p>OK\u30dc\u30bf\u30f3\u62bc\u4e0b\u306b\u3066\u547c\u3073\u51fa\u3055\u308c\u308b\u3001\u4ee5\u4e0b\u30bd\u30fc\u30b9\u306eselectBodyMass()\u30e1\u30bd\u30c3\u30c9\u3067\u4fdd\u5b58\u51e6\u7406\u3092\u5b9f\u884c<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/06\/save_bodymass.png.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" title=\"save_bodymass.png.PNG\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/06\/save_bodymass.png.png\" alt=\"save_bodymass\" width=\"163\" height=\"199\" border=\"0\" \/><\/a><\/p>\n<pre class=\"prettyprint\">import WatchKit\nimport Foundation\nimport HealthKit\n\nclass InterfaceController: WKInterfaceController {\n\n    @IBOutlet weak var bodyMassPicker: WKInterfacePicker!\n    var items:[WKPickerItem] = []\n    @IBOutlet weak var selectedBodyMassLabel: WKInterfaceLabel!\n    var selectedBodyMass:String?\n    \n    \n    @IBOutlet weak var healthKitStatus: WKInterfaceTextField!\n    \n    var healthStore : HKHealthStore?\n    \n    override func awake(withContext context: Any?) {\n        \/\/ Configure interface objects here.\n        if HKHealthStore.isHealthDataAvailable() {\n            self.healthStore = HKHealthStore()\n\n            let allTypes = Set([\n                HKQuantityType.quantityType(forIdentifier: .bodyMass)!\n            ])\n\n            self.healthStore?.requestAuthorization(toShare: allTypes, read: allTypes) { (success, error) in\n                var result = \"\"\n                if success {\n                    result = \"\u30a2\u30af\u30bb\u30b9\u8a31\u53ef: \\(String(describing: success))\"\n                } else {\n                    result = \"\\(String(describing: error?.localizedDescription))\"\n                }\n                DispatchQueue.main.async {\n                    self.healthKitStatus.setText(result)\n                }\n            }\n            \n        } else {\n            self.healthKitStatus.setText(\"\u30d8\u30eb\u30b9\u30c7\u30fc\u30bf\u5229\u7528\u4e0d\u53ef\")\n        }\n        \n        self.items.removeAll()\n        for num in stride(from: 60, through: 80, by: 0.1) {\n            let item = WKPickerItem()\n            item.title = \"\\(num)\"\n            items.append(item)\n        }\n        bodyMassPicker.setItems(items)\n        bodyMassPicker.setSelectedItemIndex(100)\n    }\n\n    override func willActivate() {\n        \/\/ This method is called when watch view controller is about to be visible to user\n    }\n    \n\n    override func didDeactivate() {\n        \/\/ This method is called when watch view controller is no longer visible\n    }\n    \n\n    @IBAction func selectBodyMass() {\n        guard let bm = Double(self.selectedBodyMass!) else {\n            return\n        }\n        let bodyMassGram = bm * 1000.0\n        let qty = HKQuantity(unit: .gram(), doubleValue: bodyMassGram)\n        let today = Date()\n        \n        let sample = HKQuantitySample(\n            type: HKSampleType.quantityType(forIdentifier: .bodyMass)!,\n            quantity: qty,\n            start: today,\n            end: today)\n        \n        self.healthStore?.save(sample){\n          (success, error) in\n            if success {\n                self.healthKitStatus.setText(\"\u767b\u9332:\\(String(describing: self.selectedBodyMass!))\")\n            } else {\n                self.healthKitStatus.setText(String(describing: error))\n            }\n        }\n    }\n    \n    @IBAction func selectBodyMassPicker(_ value: Int) {\n        self.selectedBodyMass = items[value].title\n    }\n}\n<\/pre>\n<p>\u767b\u9332\u3067\u304d\u305f\uff01\uff01<\/p>\n<p>(\u6700\u521d\u30ea\u30e5\u30fc\u30ba\u3092\u56de\u3057\u305f\u6642\u306b\u767a\u751f\u3059\u308b\u30a4\u30d9\u30f3\u30c8\u3067\u767b\u9332\u3057\u3066\u3057\u307e\u3063\u305f\u306e\u3067\u3001\u3084\u305f\u3089\u30c7\u30fc\u30bf\u304c\u767b\u9332\u3055\u308c\u3066\u3057\u307e\u3063\u305f)<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/06\/save_healthdata_success.png.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" title=\"save_healthdata_success.png.PNG\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/06\/save_healthdata_success.png.png\" alt=\"save_healthdata_success\" width=\"338\" height=\"600\" border=\"0\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.\u30d8\u30eb\u30b9\u30c7\u30fc\u30bf\u3092healthkit\u306b\u4fdd\u5b58\u3059\u308b \u30a2\u30d7\u30ea\u306f\u65b0\u3057\u3044\u30b5\u30f3\u30d7\u30eb\u3092\u4f5c\u6210\u3057\u3001HealthKit\u30b9\u30c8\u30a2\u306b\u8ffd\u52a0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3059\u3079\u3066\u306e\u30b5\u30f3\u30d7\u30eb\u30bf\u30a4\u30d7\u306f\u540c\u69d8\u306e\u624b\u9806\u3092\u8e0f\u307f\u307e\u3059\u304c\u3001\u5404\u30bf\u30a4\u30d7\u306b\u306f\u72ec\u81ea\u306e\u30d0\u30ea\u30a8\u30fc\u30b7\u30e7\u30f3\u304c\u3042\u308a\u307e\u3059 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1658,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","footnotes":""},"categories":[127,194,126],"tags":[192,130,195,129],"class_list":["post-1659","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swift","category-watchos","category-xcode","tag-healthkit","tag-swift","tag-watchos","tag-xcode"],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1659","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1659"}],"version-history":[{"count":2,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1659\/revisions"}],"predecessor-version":[{"id":1661,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1659\/revisions\/1661"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media\/1658"}],"wp:attachment":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}