{"id":1651,"date":"2022-06-08T20:25:43","date_gmt":"2022-06-08T11:25:43","guid":{"rendered":"https:\/\/www.typea.info\/blog\/?p=1651"},"modified":"2022-06-08T20:29:04","modified_gmt":"2022-06-08T11:29:04","slug":"swift_watchos_simple_app_connect_healthkit","status":"publish","type":"post","link":"https:\/\/www.typea.info\/blog\/index.php\/2022\/06\/08\/swift_watchos_simple_app_connect_healthkit\/","title":{"rendered":"Swift watchOS \u7c21\u5358\u306a\u30a2\u30d7\u30ea\u3092\u4f5c\u3063\u3066HealthKit\u306b\u63a5\u7d9a\u3057\u3066\u307f\u308b"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"YouTube video player\" src=\"https:\/\/www.youtube.com\/embed\/J_IT0aqBW1Y\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2022\/06\/04\/ios_watchos_dev_env\/\">iOS\u3068watchOS\u3068\u306e\u9023\u643a\u3092\u884c\u3063\u3066<\/a>\u3001<a href=\"https:\/\/www.typea.info\/blog\/index.php\/2022\/06\/07\/watchos_app_prestudy\/\">watchOS\u306e\u6982\u8981\u3092\u3061\u3087\u3053\u3063\u3068\u8abf\u3079\u305f<\/a>\u306e\u3067\u3001\u7c21\u5358\u306a\u30a2\u30d7\u30ea\u3092\u4f5c\u6210\u3057\u3001<a href=\"https:\/\/www.typea.info\/blog\/index.php\/2022\/05\/31\/ios_swift_healthkit\/\">HealthKit\u306b\u63a5\u7d9a<\/a>\u3057\u3066\u307f\u308b\u3002<\/p>\n<h3>1.\u753b\u9762\u69cb\u6210<\/h3>\n<p>\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3059\u308b\u3068\u3001\u30b9\u30c8\u30fc\u30ea\u30fc\u30dc\u30fc\u30c9\u306b4\u753b\u9762\u8868\u793a\u3055\u308c\u3066\u3044\u308b\u306e\u3060\u304c\u3001<a href=\"https:\/\/www.typea.info\/blog\/index.php\/2022\/06\/07\/watchos_app_prestudy\/\">\u3061\u3087\u3063\u3068\u898b\u305f\u611f\u3058<\/a>\u3060\u3068\u3001\u30e1\u30a4\u30f3\u30a2\u30d7\u30ea\u3001\u30b3\u30f3\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3001\u901a\u77e5\u306a\u3069\u3001\u5bfe\u5fdc\u3059\u308b\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306e\u57fa\u5e95\u30af\u30e9\u30b9\u3092\u898b\u308b\u3068\u30a2\u30d7\u30ea\u306e\u5f62\u614b\u3054\u3068\u306e\u30a4\u30e1\u30fc\u30b8\u306e\u3088\u3046\u3002<\/p>\n<p>\u4eca\u56de\u306f\u5de6\u4e0a\u306b\u8868\u793a\u3055\u308c\u3066\u3044\u308b\u3001<a href=\"https:\/\/developer.apple.com\/documentation\/watchkit\/wkinterfacecontroller\">WKInterfaceController<\/a>\u00a0\u3092\u7d99\u627f\u3057\u305f\u30e1\u30a4\u30f3\u30a2\u30d7\u30ea\u306b\u3001Picker\u3001\u30dc\u30bf\u30f3\u3001\u30e9\u30d9\u30eb\u3001\u30c6\u30ad\u30b9\u30c8\u30dc\u30c3\u30af\u30b9\u3092\u914d\u7f6e\u3059\u308b<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/06\/watchos_storyboard.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" title=\"watchos_storyboard.png\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/06\/watchos_storyboard.png\" alt=\"watchos_storyboard\" width=\"706\" height=\"357\" border=\"0\" \/><\/a><\/p>\n<h2>2.\u30bd\u30fc\u30b9<\/h2>\n<p>Picker\u3067\u3001\u4f53\u91cd\u3092\u9078\u629e<\/p>\n<p>\u30dc\u30bf\u30f3\u3067\u78ba\u5b9a<\/p>\n<p>\u30e9\u30d9\u30eb\u306b\u78ba\u5b9a\u3057\u305f\u4f53\u91cd\u3092\u8868\u793a<\/p>\n<p>\u30c6\u30ad\u30b9\u30c8\u30dc\u30c3\u30af\u30b9\u306b\u3001HealthKit\u306e\u72b6\u6cc1\u3092\u51fa\u529b\u3055\u305b\u308b<\/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        selectedBodyMassLabel.setText(self.selectedBodyMass)\n    }\n    \n    @IBAction func selectBodyMassPicker(_ value: Int) {\n        self.selectedBodyMass = items[value].title\n    }\n}\n<\/pre>\n<h2>3.\u30c8\u30e9\u30d6\u30eb\u30b7\u30e5\u30fc\u30c8<\/h2>\n<p>\u3068\u308a\u3042\u3048\u305a\u3001\u5b9f\u884c\u3057\u305f\u3089\u7d50\u679c\u306e\u30c6\u30ad\u30b9\u30c8\u30dc\u30c3\u30af\u30b9\u306b\u3001Missing com.apple.developer.healthkitentitlement. \u3068\u3044\u3046\u30a8\u30e9\u30fc<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/06\/healthkit_error.png.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" title=\"healthkit_error.png.PNG\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/06\/healthkit_error.png.png\" alt=\"healthkit_error\" width=\"203\" height=\"247\" border=\"0\" \/><\/a><\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/06\/healthkit_error_detail.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" title=\"healthkit_error_detail.PNG\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/06\/healthkit_error_detail.png\" alt=\"healthkit_error_detail\" width=\"200\" height=\"243\" border=\"0\" \/><\/a><\/p>\n<p>\u305d\u3046\u3044\u3048\u3070\u3001<a href=\"https:\/\/www.typea.info\/blog\/index.php\/2022\/05\/31\/ios_swift_healthkit\/\">iOS\u306e\u6642\u306b\u3001HealthKit\u306e\u8a2d\u5b9a\u3057\u305f<\/a>\u3068\u304d\u306b\u8272\u3005\u8a2d\u5b9a\u3057\u305f\u306a\u3068\u601d\u3063\u3066\u898b\u76f4\u3059\u3002<\/p>\n<p>Capabilities \u306b\u306f\u3001watchOS\u304b\u3089\u8a2d\u5b9a\u3067\u304d\u306a\u3044\u3088\u3046\u306a\u306e\u3067\u3001<\/p>\n<p>WatchKit Extension.entitlements \u306b HealthKit\u3092\u8ffd\u52a0\u3057\u3066\u3001YES\u306b\u3059\u308b\u3002<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/06\/entitlement_add_healthkit.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" title=\"entitlement_add_healthkit.png\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/06\/entitlement_add_healthkit.png\" alt=\"entitlement_add_healthkit\" width=\"668\" height=\"326\" border=\"0\" \/><\/a><\/p>\n<h2>4.\u5b9f\u884c<\/h2>\n<p>\u3061\u3083\u3093\u3068\u3001\u627f\u8a8d\u753b\u9762\u304cApple Watch\u306b\u3082\u8868\u793a\u3055\u308c\u305f\u3002<\/p>\n<p>\u6a29\u9650\u3092\u4ed8\u4e0e\u3059\u308b\u3068\u3001<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/06\/watchos_healthkit_ok.png.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" title=\"watchos_healthkit_ok.png.PNG\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/06\/watchos_healthkit_ok.png.png\" alt=\"watchos_healthkit_ok\" width=\"208\" height=\"253\" border=\"0\" \/><\/a><\/p>\n<p>\u7d50\u679c\u306b\u3001\u300c\u30a2\u30af\u30bb\u30b9\u8a31\u53ef:true\u300d\u304c\u60f3\u5b9a\u901a\u308a\u8868\u793a\u3055\u308c\u305f\uff01OK<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/06\/watchos_healthkit_ok2.png.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" title=\"watchos_healthkit_ok2.png.PNG\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/06\/watchos_healthkit_ok2.png.png\" alt=\"watchos_healthkit_ok2\" width=\"216\" height=\"263\" border=\"0\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>iOS\u3068watchOS\u3068\u306e\u9023\u643a\u3092\u884c\u3063\u3066\u3001watchOS\u306e\u6982\u8981\u3092\u3061\u3087\u3053\u3063\u3068\u8abf\u3079\u305f\u306e\u3067\u3001\u7c21\u5358\u306a\u30a2\u30d7\u30ea\u3092\u4f5c\u6210\u3057\u3001HealthKit\u306b\u63a5\u7d9a\u3057\u3066\u307f\u308b\u3002 1.\u753b\u9762\u69cb\u6210 \u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3059\u308b\u3068\u3001\u30b9\u30c8\u30fc\u30ea\u30fc\u30dc\u30fc\u30c9\u306b4\u753b\u9762\u8868\u793a\u3055\u308c\u3066\u3044\u308b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1650,"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-1651","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\/1651","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=1651"}],"version-history":[{"count":3,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1651\/revisions"}],"predecessor-version":[{"id":1655,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1651\/revisions\/1655"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media\/1650"}],"wp:attachment":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}