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];
相关推荐
00后程序员张32 分钟前
iOS App 混淆与资源保护:iOS配置文件加密、ipa文件安全、代码与多媒体资源防护全流程指南
android·安全·ios·小程序·uni-app·cocoa·iphone
咕噜签名分发冰淇淋3 小时前
内测分发是什么?
ios
2501_916007477 小时前
Transporter App 使用全流程详解:iOS 应用 ipa 上传工具、 uni-app 应用发布指南
android·ios·小程序·https·uni-app·iphone·webview
白玉cfc8 小时前
【OC】单例模式
开发语言·ios·单例模式·objective-c
Digitally10 小时前
比较 iPhone:全面比较 iPhone 17 系列
android·ios·iphone
2501_9159090614 小时前
HTTPS 错误解析,常见 HTTPS 抓包失败、443 端口错误与 iOS 抓包调试全攻略
android·网络协议·ios·小程序·https·uni-app·iphone
他们都不看好你,偏偏你最不争气1 天前
【iOS】UIViewController
开发语言·macos·ios·objective-c·cocoa
前端小超超1 天前
如何配置capacitor 打包的ios app固定竖屏展示?
前端·ios·web app
CocoaKier1 天前
AI让35岁程序员再次伟大
ios·微信小程序·aigc
库奇噜啦呼1 天前
【iOS】单例模式
ios·单例模式