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];
相关推荐
若水无华17 小时前
fiddler 配置ios手机代理调试
ios·智能手机·fiddler
Aress"18 小时前
【ios越狱包安装失败?uniapp导出ipa文件如何安装到苹果手机】苹果IOS直接安装IPA文件
ios·uni-app·ipa安装
Jouzzy1 天前
【iOS安全】Dopamine越狱 iPhone X iOS 16.6 (20G75) | 解决Jailbreak failed with error
安全·ios·iphone
瓜子三百克1 天前
采用sherpa-onnx 实现 ios语音唤起的调研
macos·ios·cocoa
左钦杨1 天前
IOS CSS3 right transformX 动画卡顿 回弹
前端·ios·css3
努力成为包租婆1 天前
SDK does not contain ‘libarclite‘ at the path
ios
安和昂2 天前
【iOS】Tagged Pointer
macos·ios·cocoa
I烟雨云渊T2 天前
iOS 阅后即焚功能的实现
macos·ios·cocoa
struggle20252 天前
适用于 iOS 的 开源Ultralytics YOLO:应用程序和 Swift 软件包,用于在您自己的 iOS 应用程序中运行 YOLO
yolo·ios·开源·app·swift
Unlimitedz2 天前
iOS视频编码详细步骤(视频编码器,基于 VideoToolbox,支持硬件编码 H264/H265)
ios·音视频