Open6
【HealthKit】WatchOSにおけるWorkout
WWDC
Build a workout app for Apple Watch
を参考に進める。
このスクラップにまとめている内容とほぼ一緒。
ただ、サンプルコードが存在し処理が追いやすいので振り返る。
まずは、権限のリクエストを実施。
func requestAuthorization() {
let typesToShare: Set = [HKQuantityType.workoutType()]
let typesToRead: Set = [
HKQuantityType.quantityType(forIdentifier: .heartRate)!,
HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned)!,
HKQuantityType.quantityType(forIdentifier: .distanceWalkingRunning)!,
HKQuantityType.quantityType(forIdentifier: .distanceCycling)!,
HKObjectType.activitySummaryType()
]
healthStore.requestAuthorization(toShare: typesToShare, read: typesToRead) { (success, error) in
if !success {
print("HealthKitのアクセス許可が拒否されました")
}
}
}
Workoutを開始
private func startWorkout(workoutType: HKWorkoutActivityType) {
let configuration = HKWorkoutConfiguration()
configuration.activityType = workoutType
configuration.locationType = .outdoor
do {
session = try HKWorkoutSession(healthStore: healthStore, configuration: configuration)
builder = session?.associatedWorkoutBuilder()
} catch {
print("ワークアウトセッションの作成に失敗しました: \(error.localizedDescription)")
return
}
session?.delegate = self
builder?.delegate = self
builder?.dataSource = HKLiveWorkoutDataSource(healthStore: healthStore, workoutConfiguration: configuration)
let startDate = Date()
session?.startActivity(with: startDate)
builder?.beginCollection(withStart: startDate) { (success, error) in
print("ワークアウトが開始されました")
}
}
HKWorkoutSessionDelegate
メソッドで、セッションの状態を監視する
extension WorkoutManager: HKWorkoutSessionDelegate {
func workoutSession(_ workoutSession: HKWorkoutSession, didChangeTo toState: HKWorkoutSessionState,
from fromState: HKWorkoutSessionState, date: Date) {
DispatchQueue.main.async {
self.running = toState == .running
}
if toState == .ended {
builder?.endCollection(withEnd: date) { (success, error) in
self.builder?.finishWorkout { (workout, error) in
DispatchQueue.main.async {
self.workout = workout
}
}
}
}
}
func workoutSession(_ workoutSession: HKWorkoutSession, didFailWithError error: Error) {
print("ワークアウトセッションでエラーが発生しました: \(error.localizedDescription)")
}
}
Workoutのライブデータの状態を監視する。
イベント・心拍などのヘルスデータなど
extension WorkoutManager: HKLiveWorkoutBuilderDelegate {
func workoutBuilderDidCollectEvent(_ workoutBuilder: HKLiveWorkoutBuilder) {
print("イベントが収集されました")
}
func workoutBuilder(_ workoutBuilder: HKLiveWorkoutBuilder, didCollectDataOf collectedTypes: Set<HKSampleType>) {
for type in collectedTypes {
guard let quantityType = type as? HKQuantityType else {
return
}
let statistics = workoutBuilder.statistics(for: quantityType)
updateForStatistics(statistics)
}
}
}