菜鸟教程小白 发表于 2022-12-12 23:01:16

ios - 是否可以从 HealthKit 读取 Apple Watch 目标(移动、跨步和站立)?


                                            <p><p>是否可以从 HealthKit 读取 Apple Watch 移动目标?</p>

<p>我可以使用数量标识符 HKQuantityTypeIdentifier.activeEnergyBurned 检索移动值。我找不到移动目标的类似标识符。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><pre><code>//Declared globally
var healthStore: HKHealthStore?

func prepareHealthKit() {
    guard HKHealthStore.isHealthDataAvailable() else {
      return
    }

    var readTypes = Set&lt;HKObjectType&gt;()
    readTypes.insert(HKObjectType.activitySummaryType())

    healthStore = HKHealthStore()
    healthStore!.requestAuthorization(toShare: nil, read: readTypes) { (isSuccess, error) in
      /*
         Assuming you know the following steps:
         1. Start workout session: i.e. &#34;HKWorkoutSession&#34;
         2. Wait for delegate: i.e &#34;workoutSession(_:didChangeTo:from:date:)&#34;
         3. Start Query for Activity Summary in the delegate:
            i.e our &#34;startQueryForActivitySummary()&#34;
         */
    }
}
</code></pre>

<hr/>

<pre><code>func startQueryForActivitySummary() {
    func createPredicate() -&gt; NSPredicate? {
      let calendar = Calendar.autoupdatingCurrent

      var dateComponents = calendar.dateComponents([.year, .month, .day],
                                                   from: Date())
      dateComponents.calendar = calendar

      let predicate = HKQuery.predicateForActivitySummary(with: dateComponents)
      return predicate
    }

    let queryPredicate = createPredicate()
    let query = HKActivitySummaryQuery(predicate: queryPredicate) { (query, summaries, error) -&gt; Void in
      if let summaries = summaries {
            for summary in summaries {
                let activeEnergyBurned = summary.activeEnergyBurned.doubleValue(for: HKUnit.kilocalorie())
                let activeEnergyBurnedGoal = summary.activeEnergyBurnedGoal.doubleValue(for: HKUnit.kilocalorie())
                let activeEnergyBurnGoalPercent = round(activeEnergyBurned/activeEnergyBurnedGoal)

                print(activeEnergyBurnGoalPercent)
            }
      }
    }

    healthStore?.execute(query)
}
</code></pre>

<hr/>

<p>引用资料:</p>

<ul>
<li> <a href="https://crunchybagel.com/accessing-activity-rings-data-from-healthkit" rel="noreferrer noopener nofollow">https://crunchybagel.com/accessing-activity-rings-data-from-healthkit</a> </li>
<li> <a href="https://developer.apple.com/documentation/healthkit/hkactivitysummary" rel="noreferrer noopener nofollow">https://developer.apple.com/documentation/healthkit/hkactivitysummary</a> </li>
<li> <a href="https://developer.apple.com/documentation/healthkit/hkactivitysummaryquery" rel="noreferrer noopener nofollow">https://developer.apple.com/documentation/healthkit/hkactivitysummaryquery</a> </li>
</ul></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 是否可以从 HealthKit 读取 Apple Watch 目标(移动、跨步和站立)?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/40861634/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/40861634/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 是否可以从 HealthKit 读取 Apple Watch 目标(移动、跨步和站立)?