📝

WWDC24 『visionOSでのHealthKitの導入』の視聴メモ

に公開

HeakthKitがvisionOS 2から導入された

  • iPadにはiPadOS 17から導入された
  • Apple Vision ProにはvisionOS 2から導入された

onGeometryChange(for:of:action:)でグラフを動的にサイズ変更する

// Update number of chart points based on chart’s size

import SwiftUI
import HealthKit
import Charts

struct ChartView: View {
    @State var chartBinCount: Int

    var body: some View {
        Chart { ...
            // Chart body
        }
        .onGeometryChange(for: Int.self) { proxy in // Observe for changes to the chart’s size
            Int(proxy.size.width / 80) // 80 points per chart point
        } action: { newValue in
            // Update the number of chart points
            chartBinCount = newValue        
        }
    }
}

マルチウインドウのサポート方法

https://developer.apple.com/documentation/SwiftUI/bringing-multiple-windows-to-your-swiftui-app

ゲストユーザー

HealthKitの承認を要求するとエラーになる

  • すでに承認されていればHealthKitデータにアクセスできる
// Request authorization to State of Mind datatype

@main
struct HKStateOfMindDataSampleApp: App {
    @State var toggleHealthDataAuthorization = false
    @State var healthDataAuthorized: Bool?
    
    var body: some Scene {
        WindowGroup {
            TabView { ... }
                .healthDataAccessRequest(store: healthStore,
                                         shareTypes: [.stateOfMindType()],
                                         readTypes: [.stateOfMindType()],
                                         trigger: toggleHealthDataAuthorization) { result in
                    switch result {
                    case .success: healthDataAuthorized = true
                    case .failure(let error as HKError):
                        switch (error.code) {
                        case .errorNotPermissibleForGuestUserMode:
                            // Defer requests for a later time
                        default:
                            // Existing error handling
                        }
                        ...
                    }
                }
        }
    }
}

HealthKitデータの書き込みをするとエラーになる

// Saves a State of Mind sample from an emoji type 
public func saveSample(date: Date,
                       association: HKStateOfMind.Association,
                       healthStore: HKHealthStore,
                       didError: Binding<Bool>) async -> SaveDetails? {
    do {
        let sample = createSample(date: date, association: association)
        try await healthStore.save(sample)
    } catch {
        switch error {
        case HKError.errorNotPermissibleForGuestUserMode:
            // Drop data you generate in a Guest User session.
            didError.wrappedValue = true
            return SaveDetails(errorString: "Health data is not saved for Guest Users.")
        default:
            // Existing error handling.
            didError.wrappedValue = true
            return SaveDetails(errorString: "Health data could not be saved: \(error)")
        }
    }
...

参考資料

https://developer.apple.com/jp/videos/play/wwdc2024/10083

Discussion