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];
相关推荐
麦兜*3 小时前
Swift + Xcode 开发环境搭建终极指南
开发语言·ios·swiftui·xcode·swift·苹果vision pro·swift5.6.3
Digitally8 小时前
重置iPhone会删除所有内容吗? 详细回答
ios·iphone
普罗米拉稀10 小时前
Flutter 复用艺术:Mixin 与 Abstract 的架构哲学与线性化解密
flutter·ios·面试
kymjs张涛17 小时前
零一开源|前沿技术周刊 #12
ios·google·github
2501_915918411 天前
iOS 应用上架全流程实践,从开发内测到正式发布的多工具组合方案
android·ios·小程序·https·uni-app·iphone·webview
笔沫拾光1 天前
iOS 正式包签名指南
flutter·ios·ios签名
Magnetic_h2 天前
【iOS】锁的原理
笔记·学习·macos·ios·objective-c·cocoa·xcode
Digitally2 天前
将 iPhone 联系人转移到 Infinix 的完整指南
ios·cocoa·iphone
imLix2 天前
RunLoop 实现原理
前端·ios
归辞...3 天前
「iOS」————设计架构
ios·架构