iOS health kit获取当天数据 少八小时的问题

龙渊之源 2016-06-23 11:14:15
各位,近来闲着无聊就看了一下iOS的healthkit的功能,然后在看了一下官方文档后就开始编写了一个小的demo。可是在demo编写完成后发现,在获得到health的权限后获取数据的时候居然是从八点后开始获得到数据,从而每次获取当天的数据时候就会少八小时,而获取全部数据的时候是正确的。后来我看了博客后发现说是自己自定义一下就可以了。可是我在写了一个当天时间的零点开始方法和当天结束时间后发现它还是从八点后开始获取数据,这个事情有人遇到过吗?又是怎么解决的啊
#import "SecondViewController.h"
#import <HealthKit/HealthKit.h>

@interface SecondViewController ()

@property (nonatomic, strong) HKHealthStore *healthStore;

@end

@implementation SecondViewController

- (void)viewDidLoad {
[super viewDidLoad];

if(![HKHealthStore isHealthDataAvailable])
{
NSLog(@"设备不支持");
}

self.healthStore = [[HKHealthStore alloc] init];

HKObjectType *stepCount = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];

NSSet *healthSet = [NSSet setWithObjects:stepCount, nil];

[self.healthStore requestAuthorizationToShareTypes:nil readTypes:healthSet completion:^(BOOL success, NSError * _Nullable error) {
if (success)
{
NSLog(@"获取步数权限成功");
[self readStepCount];
}
else
{
NSLog(@"获取步数权限失败");
}
}];

}

- (void)readStepCount
{
HKSampleType *sampleType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];

NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:[self getStartTime] endDate:[self getEndTime] options:HKQueryOptionNone];
NSSortDescriptor *start = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierStartDate ascending:NO];
NSSortDescriptor *end = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierEndDate ascending:NO];
HKSampleQuery *sampleQuery = [[HKSampleQuery alloc] initWithSampleType:sampleType predicate:predicate limit:0 sortDescriptors:@[start,end] resultsHandler:^(HKSampleQuery * _Nonnull query, NSArray<__kindof HKSample *> * _Nullable results, NSError * _Nullable error) {
if(!error && results){
NSLog(@"%@",results);
int sum=0;
for (int i=0; i<results.count; i++) {
HKQuantitySample *result = results[i];
HKQuantity *quantity = result.quantity;
NSString *stepStr = (NSString *)quantity;
NSString *aaaa = [[NSString stringWithFormat:@"%@",stepStr] stringByReplacingOccurrencesOfString:@" count" withString:@""];
sum+= [aaaa intValue];
}
NSLog(@"%d",sum);
}

}];

[self.healthStore executeQuery:sampleQuery];
}

/**
获取当前时区的时间
*/
-(NSDate *)getEndTime{
NSDate *date = [[NSDate alloc]init];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate:date];
NSDate *nowDate = [date dateByAddingTimeInterval:interval];
return nowDate;
}

/**
获取开始时间 当天0时0分0秒
*/
-(NSDate *) getStartTime{
NSDateFormatter *datef = [[NSDateFormatter alloc]init];
datef.dateFormat = @"yyyy-MM-dd";
NSString *stringdate = [datef stringFromDate:[self getEndTime]];
NSDate *tdate = [datef dateFromString:stringdate];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate:tdate];
NSDate *nowday = [tdate dateByAddingTimeInterval:interval];
return nowday;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
...全文
212 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
不担心 2016-06-27
  • 打赏
  • 举报
回复
NSTimeZone *zone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];试试

29,049

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧