各位,近来闲着无聊就看了一下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
解决方案
20
NSTimeZone *zone = [NSTimeZone timeZoneWithAbbreviation:@”UTC”];试试