如何获取系统健康程序的权限和信息
来源:互联网 发布:嘉庚学校域名 编辑:程序博客网 时间:2024/05/08 05:40
1.首先,我们需要在xcode 中设置HealthKit的开关
2.在需要获取健康信息的地方导入# import<HealthKit/HealthKit.h>
@interface
ViewController ()
@property
(
nonatomic
, strong) HKHealthStore *healthStore;
@end
在- (void)viewDidLoad中获取权限
- (
void
)viewDidLoad {
[
super
viewDidLoad];
//查看healthKit在设备上是否可用,ipad不支持HealthKit
if
(![HKHealthStore isHealthDataAvailable])
{
NSLog
(@
"设备不支持healthKit"
);
}
//创建healthStore实例对象
self
.healthStore = [[HKHealthStore alloc] init];
//设置需要获取的权限这里仅设置了步数
HKObjectType *stepCount = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
NSSet
*healthSet = [
NSSet
setWithObjects:stepCount,
nil
];
//从健康应用中获取权限
[
self
.healthStore requestAuthorizationToShareTypes:
nil
readTypes:healthSet completion:^(
BOOL
success,
NSError
* _Nullable error) {
if
(success)
{
NSLog
(@
"获取步数权限成功"
);
//获取步数后我们调用获取步数的方法
[
self
readStepCount];
}
else
{
NSLog
(@
"获取步数权限失败"
);
}
}];
}
读取步数
//查询数据
- (
void
)readStepCount
{
//查询采样信息
HKSampleType *sampleType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
//NSSortDescriptors用来告诉healthStore怎么样将结果排序。
NSSortDescriptor
*start = [
NSSortDescriptor
sortDescriptorWithKey:HKSampleSortIdentifierStartDate ascending:
NO
];
NSSortDescriptor
*end = [
NSSortDescriptor
sortDescriptorWithKey:HKSampleSortIdentifierEndDate ascending:
NO
];
/*查询的基类是HKQuery,这是一个抽象类,能够实现每一种查询目标,这里我们需要查询的步数是一个
HKSample类所以对应的查询类就是HKSampleQuery。
下面的limit参数传1表示查询最近一条数据,查询多条数据只要设置limit的参数值就可以了
*/
HKSampleQuery *sampleQuery = [[HKSampleQuery alloc] initWithSampleType:sampleType predicate:
nil
limit:1 sortDescriptors:@[start,end] resultsHandler:^(HKSampleQuery * _Nonnull query,
NSArray
<__kindof HKSample *> * _Nullable results,
NSError
* _Nullable error) {
//打印查询结果
NSLog
(@
"resultCount = %ld result = %@"
,results.count,results);
//把结果装换成字符串类型
HKQuantitySample *result = results[0];
HKQuantity *quantity = result.quantity;
NSString
*stepStr = (
NSString
*)quantity;
[[
NSOperationQueue
mainQueue] addOperationWithBlock:^{
//查询是在多线程中进行的,如果要对UI进行刷新,要回到主线程中
NSLog
(@
"最新步数:%@"
,stepStr);
}];
}];
//执行查询
[
self
.healthStore executeQuery:sampleQuery];
}
0 0
- 如何获取系统健康程序的权限和信息
- iOS 获取健康里的信息(步数和公里数)
- ls小程序,使用到文件信息和权限等的获取
- C#获取用户系统信息的程序
- 如何获取系统信息
- 十,iOS 健康数据获取权限和写入权限
- 如何获取系统的串口信息
- 如何获取系统默认的时区信息
- 复制自身程序到windows目录和系统目录,获取系统的相关信息
- Android程序的安全系统 获取系统权限
- 应用程序如何获取系统权限
- 应用程序如何获取系统权限
- 复制程序,获取系统信息
- Android获取程序详细权限信息
- 如何获取用户和手机的信息
- 如何获取ios系统信息
- 刚装好的linux系统如何获取root的权限
- 程序中如何获取Android的Root权限
- webrtc直播音频调节失效(小米等部分机型)
- JNI之C语言(三)-- 基本语法
- Android studio 使用aidl实现远程服务
- 内核线程、软中断和定时器有何区别
- 山东省第七届ACM大学生程序设计竞赛-Execution of Paladin(炉石传说-模拟)
- 如何获取系统健康程序的权限和信息
- 补欠账
- spring aop技术 学习总结
- JS数组
- Spring缓存与声明式事务管理
- Union的迷思
- 两则C语言技巧
- JNI之C语言(四)-- 函数
- 翻转单词顺序 VS 左旋转字符串43