HealthKit学习

HealthKit是iOS8的特性,用来提供存储和获取用户健康数据

1。获取HealthKit的授权,在Targets-Capabilities打开HealthKit的开关

  • 如果是app支持iOS8以下或者需要支持pad等不支持healthKit,需要在info。plist里删除掉healthKit的选项,或者会被苹果拒
    并请求这些应用装不上App
    如下图
2.png

最后的配置图下

1.png

2。指定HealthKit的数据类型,并授权

HealthKit的数据类型都是HKObjectType的子类,提供了5个方法用来获取HKObjectType子类的类型,再

  public class func quantityTypeForIdentifier(identifier: String) -> HKQuantityType?
  public class func categoryTypeForIdentifier(identifier: String) -> HKCategoryType?
  public class func characteristicTypeForIdentifier(identifier: String) -> HKCharacteristicType?
  public class func correlationTypeForIdentifier(identifier: String) -> HKCorrelationType?
  public class func workoutType() -> HKWorkoutType

identifier类型可通过https://developer.apple.com/library/watchos/documentation/HealthKit/Reference/HealthKit_Constants/#//apple_ref/doc/constant_group/Body_Measurements 查看

授权代码

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        healthStore = HKHealthStore()
        if HKHealthStore.isHealthDataAvailable() {
            self.authorizeHealthKit()
        }

    }

    func authorizeHealthKit() {

        let readType:Set<HKObjectType> = [HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!]
        let writeType:Set<HKSampleType> = [HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!]

        //获取授权
        self.healthStore.requestAuthorizationToShareTypes(writeType, readTypes: readType) { (x:Bool, y:NSError?) -> Void in

        }

    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • HealthKit框架学习 本文结构 简介 用户数据安全及隐私 HealthKit框架介绍 HealthKit使用...
    奇哥Dodge阅读 9,341评论 4 11
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,366评论 4 61
  • 转载自:https://github.com/Tim9Liu9/TimLiu-iOS 目录 UI下拉刷新模糊效果A...
    袁俊亮技术博客阅读 14,117评论 9 105
  • 自从加入特种兵特训营后,每天高强度的作业,和顾客沟通、和代理沟通等等,接送孩子、买菜做饭、送货、辅导作业,我都快成...
    兰馨0210阅读 1,796评论 0 0
  • 时维九月,序属三秋。 天气渐渐凉了下来。 早上七点钟的阳光恢复了一贯的恬淡,不再毒辣地让人想到出门上班就寻死觅活。...
    南蓂阅读 3,596评论 4 4

友情链接更多精彩内容