获取android手机步数,获取手机健康应用中的步数和距离

废话不多说,直接上步骤

首先新建一个类HealthKitManage,采用单利模式

HealthKitManage.h中导入

#import

#import

1.定义

#define HKVersion [[[UIDevice currentDevice] systemVersion] doubleValue]

#define CustomHealthErrorDomain @"com.sdqt.healthError"

2.定义几个方法

+(id)shareInstance; //单例

- (void)authorizeHealthKit:(void(^)(BOOL success, NSError *error))compltion;

- (void)getStepCount:(void(^)(double value, NSError *error))completion; //获取步数

- (void)getDistance:(void(^)(double value, NSError *error))completion; //获取距离

HealthKitManage.m中

1.单例

+(id)shareInstance{ //单例

static id manager ;

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

manager = [[[self class] alloc] init];

});

return manager;

}

2.实现其他方法

/*

* @brief 检查是否支持获取健康数据

*/

- (void)authorizeHealthKit:(void(^)(BOOL success, NSError *error))compltion{

if(HKVersion >= 8.0){

if (![HKHealthStore isHealthDataAvailable]) {

NSError *error = [NSError errorWithDomain: @"com.raywenderlich.tutorials.healthkit" code: 2 userInfo: [NSDictionary dictionaryWithObject:@"HealthKit is not available in th is Device" forKey:NSLocalizedDescriptionKey]];

if (compltion != nil) {

compltion(false, error);

}

return;

}

if ([HKHealthStore isHealthDataAvailable]) {

if(self.healthStore == nil)

self.healthStore = [[HKHealthStore alloc] init];

/*

组装需要读写的数据类型

*/

NSSet *writeDataTypes = [self dataTypesToWrite];

NSSet *readDataTypes = [self dataTypesRead];

/*

注册需要读写的数据类型,也可以在“健康”APP中重新修改

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值