【问题标题】:Xamarin iOS Reading Step Counts from HealthKitXamarin iOS 从 HealthKit 读取步数
【发布时间】:2017-10-03 13:56:59
【问题描述】:

我正在尝试从用户那里及时读取 365 天的步数,然后将其上传到服务器。但是我目前卡在提取数据上,我正确获得了 iOS healthkit 的许可,但是我的数据的返回类型只是 get "[0:] HealthKit.HKSample[]"

public void GetSteps()
{        
    var healthKitStore = new HKHealthStore();
    var stepRateType = HKQuantityType.Create(HKQuantityTypeIdentifier.StepCount);
    var sort = new NSSortDescriptor(HKSample.SortIdentifierStartDate, true);
    var q = new HKSampleQuery(stepRateType, HKQuery.GetPredicateForSamples(NSDate.Now.AddSeconds(TimeSpan.FromDays(-365).TotalSeconds), NSDate.Now.AddSeconds(TimeSpan.FromDays(1).TotalSeconds), HKQueryOptions.None), 0, new NSSortDescriptor[] { },
        new HKSampleQueryResultsHandler((HKSampleQuery query2,HKSample[] results, NSError error2) =>
        {
            var query = results; //property created within the model to expose later.
            Debug.WriteLine(query);
            Debug.WriteLine(results);
        }));
    healthKitStore.ExecuteQuery(q); 
}

【问题讨论】:

标签: c# xamarin xamarin.ios healthkit


【解决方案1】:

我想我知道你为什么得到“[0:] HealthKit.HKSample[]”,你正在尝试 Debug.WriteLine 一个对象数组。结果变量是一个数组。而是循环遍历数组,并在其他可用字段中提取“数量”、“开始日期”和“结束日期”:

foreach (var item in results)
{
    var sample = (HKQuantitySample) item;
    var hkUnit = HKUnit.Count;
    var quantity = sample.Quantity.GetDoubleValue(hkUnit);
    var startDateTime = sample.StartDate.ToDateTime().ToLocalTime();
    var endDateTime = sample.EndDate.ToDateTime().ToLocalTime();
    Debug.WriteLine(quantity);
    Debug.WriteLine(startDateTime);
    Debug.WriteLine(endDateTime);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多