我正在尝试使用iOS 8.1.2对iPhone 6上的计步器缓存进行查询,我使用的是Objective-c,我已经导入了CoreMotion框架,并将其包含在项目中,代码看起来像这样
NSDate *startDate = [[NSDate date] dateByAddingTimeInterval:-60*60*12];
NSDate *endDate = [NSDate date];
CMPedometer *pedo = [[CMPedometer alloc]init];
[pedo queryPedometerDataFromDate:startDate toDate:endDate withHandler:^(CMPedometerData *pedometerData, NSError *error)
{
if (error)
{
NSLog(@"error: %@", error);
}
}];
Run Code Online (Sandbox Code Playgroud)
这给了我错误:错误域= CMErrorDomain代码= 103"操作无法完成.(CMErrorDomain错误103.)"
如果我在这样的Swift中做同样的事情
var dateString = "2014-12-15"
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "YYYY-MM-DD"
var startDate = dateFormatter.dateFromString(dateString)
var endDate = NSDate()
pedometer.queryPedometerDataFromDate(startDate, toDate: endDate){
(data, error) -> Void in
if error != nil
{
println("There was an error requesting data from the pedometer: …Run Code Online (Sandbox Code Playgroud) 您好我正在尝试设置启用了后台传递的运行状况存储观察器.我的问题是,当屏幕被锁定时,它不会传递任何东西.我已经简化了我的代码来解决这个问题:)我在我的plist中有HealthKit并且我接受了healthStore类型的步数.当应用程序打开并且屏幕未锁定时,一切都很好.但是当屏幕被锁定时,我没有得到任何观察.出于测试目的,频率设置为立即.
我的代码如下
- (void)setupHealthStore{
if ([HKHealthStore isHealthDataAvailable])
{
NSSet *readDataTypes = [self dataTypesToRead];
self.healthStore = [[HKHealthStore alloc]init];
[self.healthStore requestAuthorizationToShareTypes:nil readTypes:readDataTypes completion:^(BOOL success, NSError *error)
{
if (success)
{
HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
[self.healthStore enableBackgroundDeliveryForType:quantityType frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError *error)
{
if (success)
{
[self setupObserver];
}
}];
}
}];
}
Run Code Online (Sandbox Code Playgroud)
}
在AppDelegate didfinishLaunchWithOptions中调用上述方法
下一个方法是
- (void)setupObserver{
HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
HKObserverQuery *query = [[HKObserverQuery alloc]initWithSampleType:quantityType predicate:nil updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError *error)
{
if (!error)
{
[self alarm]; …Run Code Online (Sandbox Code Playgroud)