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];
相关推荐
美狐美颜sdk7 小时前
跨平台直播美颜SDK集成实录:Android/iOS如何适配贴纸功能
android·人工智能·ios·架构·音视频·美颜sdk·第三方美颜sdk
恋猫de小郭12 小时前
Meta 宣布加入 Kotlin 基金会,将为 Kotlin 和 Android 生态提供全新支持
android·开发语言·ios·kotlin
泓博12 小时前
Objective-c把字符解析成字典
开发语言·ios·objective-c
Daniel_Coder13 小时前
Xcode 中常用图片格式详解
ios·xcode·swift
瓜子三百克13 小时前
Objective-C 路由表原理详解
开发语言·ios·objective-c
帅次14 小时前
Objective-C面向对象编程:类、对象、方法详解(保姆级教程)
flutter·macos·ios·objective-c·iphone·swift·safari
RyanGo16 小时前
iOS断点下载
ios·swift
蒙小萌199316 小时前
找工作-iOS开发-3年经验-AI协作开发
ios
丶皮蛋菌19 小时前
关于OC与Swift内存管理的解惑
ios
杂雾无尘20 小时前
掌握生死时速:苹果应用加急审核全攻略!
ios·swift·apple