佚名通过本文主要向大家介绍了sql获取当天数据,oracle 获取当天数据,mysql获取当天数据,oracle查询当天数据,mysql查询当天数据等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
问题: iOS health kit获取当天数据 少八小时的问题
描述:
解决方案1:
描述:
本帖最后由 jeffahjd 于 2016-06-23 11:15:46 编辑
healthkit步行数
各位,近来闲着无聊就看了一下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
解决方案1:
NSTimeZone *zone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];试试