iOS 苹果健康-睡眠

直接上代码吧

api如下

c 复制代码
HKCategoryType *sleep1 = [HKCategoryType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis]; 
 //睡眠分析
NSSet *healthSet = [NSSet setWithObjects:sleep1,nil];
//向系统的健康APP请求数据,并回调结果
        [self.healthStore requestAuthorizationToShareTypes:nil readTypes:healthSet completion:^(BOOL success, NSError * _Nullable error) {
            if (success) {
                NSLog(@"requestAuthorization success");
                [self readHealthData:HKCategoryTypeIdentifierSleepAnalysis];
            }else{
            NSLog(@"requestAuthorization error");
            }
        }];


    //查询采样信息
    HKSampleType *sampleType = [HKCategoryType categoryTypeForIdentifier:HKQuantityTypeIdentifierType];
    
    //NSSortDescriptors用来告诉healthStore怎么样将结果排序。
    NSSortDescriptor *start = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierStartDate ascending:NO];
    NSSortDescriptor *end = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierEndDate ascending:NO];
    /*
     查询的基类是HKQuery,这是一个抽象类,能够实现每一种查询目标,这里我们需要查询的步数是一个
     HKSample类所以对应的查询类就是HKSampleQuery。
     下面的limit参数传1表示查询最近一条数据,查询多条数据只要设置limit的参数值就可以了
     在这里我们需要定义block里面执行哪些语句
     */
    HKSampleQuery *sampleQuery = [[HKSampleQuery alloc] initWithSampleType:sampleType predicate:nil limit:100 sortDescriptors:@[start,end] resultsHandler:^(HKSampleQuery * _Nonnull query, NSArray<__kindof HKSample *> * _Nullable results, NSError * _Nullable error) {
        //打印查询结果
        NSLog(@"健康 resultCount = %ld result = %@",results.count,results);
        if (results.count!=0) {
            HKCategorySample *sample = results[0];
            
            for (HKCategorySample *sam in results) {
                NSLog(@"健康  %@",@(sam.value));
                // 假设你有一个时间戳
                NSDate *start = sam.startDate;
                NSDate *end = sam.endDate;
                if(sam.value == HKCategoryValueSleepAnalysisInBed){
                    NSLog(@"健康 卧床时间: 开始时间:%@ --- 结束时间:%@",start,end);
                }
                else if (sam.value == HKCategoryValueSleepAnalysisAsleepUnspecified){
                    NSLog(@"健康 睡眠时间: 开始时间:%@ --- 结束时间:%@",start,end);
                }
                else if (sam.value == 2){
                    NSLog(@"健康 清醒时间: 开始时间:%@ --- 结束时间:%@",start,end);
                }
                else if (sam.value == 3){
                    NSLog(@"健康 核心睡眠时间: 开始时间:%@ --- 结束时间:%@",start,end);
                }
                else if (sam.value == 4){
                    NSLog(@"健康 深度睡眠时间: 开始时间:%@ --- 结束时间:%@",start,end);
                }
                else if (sam.value == 5){
                    NSLog(@"健康 快速动眼时间: 开始时间:%@ --- 结束时间:%@",start,end);
                }

            }
        }
    }];
    //执行查询
    [self.healthStore executeQuery:sampleQuery];
相关推荐
张较瘦_4 分钟前
[论文阅读] 人工智能 + 软件工程 | 大模型破局跨平台测试!LLMRR让iOS/安卓/鸿蒙脚本无缝迁移
论文阅读·人工智能·ios
m0_6410310510 小时前
在选择iOS代签服务前,你必须了解的三大安全风险
ios
开开心心loky11 小时前
[iOS] push 和 present Controller 的区别
ui·ios·objective-c·cocoa
白玉cfc16 小时前
【iOS】push,pop和present,dismiss
macos·ios·cocoa
低调小一17 小时前
iOS 开发入门指南-HelloWorld
ios
2501_9159184117 小时前
iOS 开发全流程实战 基于 uni-app 的 iOS 应用开发、打包、测试与上架流程详解
android·ios·小程序·https·uni-app·iphone·webview
用户870568130451 天前
iOS 异步渲染:从 CALayer 切入的实现与优化
ios
敲代码的鱼哇2 天前
跳转原生系统设置插件 支持安卓/iOS/鸿蒙UTS组件
android·ios·harmonyos
在下历飞雨2 天前
Kuikly基础之状态管理与数据绑定:让“孤寡”计数器动起来
ios·harmonyos
在下历飞雨2 天前
Kuikly基础之Kuikly DSL基础组件实战:构建青蛙主界面
ios·harmonyos