我正在尝试记录和读取 Apple Watch 的历史数据。 不幸的是,关联的 DataList 总是等于 nil。
更明确地说,在我的 watch 应用程序上,我有两个按钮: - 一个开始记录数据 - 一个读取数据
这是相关代码 - 在 WatchKit 扩展上运行:
- (IBAction)start {
// Check authorizations with boolean
bool toTest_AccelerometerAvailable = [CMSensorRecorder isAccelerometerRecordingAvailable];
bool toTest_RecordingAuthorized = [CMSensorRecorder isAuthorizedForRecording];
// Check if recorder has been initialized, then launch record session for 20 seconds
if ([CMSensorRecorder isAuthorizedForRecording]) {
if (!self.recorder)
{
self.recorder = [[CMSensorRecorder alloc] init];
}
NSTimeInterval interval = 1 * 20;
[self.recorder recordAccelerometerFor:interval];
}
}
- (IBAction)read {
// Check authorizations with boolean
bool toTest_AccelerometerAvailable = [CMSensorRecorder isAccelerometerRecordingAvailable];
bool toTest_RecordingAuthorized = [CMSensorRecorder isAuthorizedForRecording];
// Try to get historical data from the last 2 days
NSDate *now = [NSDate date];
NSDate *startDate = [now dateByAddingTimeInterval:-2*24*60*60];
CMSensorDataList *dataList = [self.recorder accelerometerDataFrom:startDate to:[NSDate date]];
}
在 iOS 应用端,我要求提供表格和健身授权。
调试 session 给出了以下结果:
boolean toTest_RecordingAuthorized 始终等于 true(启动和读取功能)- 在关联的 iOS 应用中请求访问表单和健身。
boolean toTest_AccelerometerAvailable 始终等于 false(启动和读取函数)
CMSensorDataList *dataList 在任何时候都等于 nil
测试是用模拟器和真实设备(iOS 9.0 和 watchOS 2.0)进行的。结果一样!
我想,只要 isAccelerometerRecordingAvailable 返回 false,问题就会一直存在。 不知道如何解决...
有没有人也尝试过这个问题并解决它?
最佳答案
-2*24*60*60
,您正在尝试访问最近 2 天的数据,但根据文档,您只能查询 12 小时的数据。在这里阅读:
Apple Documentation about CMSensorRecorder
关于objective-c - 使用 watchOS2 读取加速度计数据 - iOS9 - XCode 7.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32722652/