스트리밍은 대부분의 브라우저와
Developer 앱에서 사용할 수 있습니다.
-
HealthKit의 웰빙 API 살펴보기
HealthKit을 사용해 정신 건강 및 웰빙 관련 콘텐츠를 앱과 통합하는 방법을 알아봅니다. 마음 상태는 물론, 우울증 및 불안증 발생 위험을 파악하기 위한 새로운 API를 살펴볼 수 있습니다. 감정에 대한 과학적 접근 원칙을 자세히 살펴보고, 감정을 살피는 것이 얼마나 유익할 수 있는지, 다른 기분 및 감정 유형을 표현하기 위해 마음 상태를 사용하는 방법 등을 알아봅니다.
챕터
- 0:00 - Introduction
- 1:28 - Mental wellbeing APIs
- 2:13 - State of Mind API
- 8:54 - State of Mind predicates
리소스
관련 비디오
WWDC24
WWDC20
-
다운로드Array
-
-
5:37 - Request authorization to read and write State of Mind HealthKit samples
// Request authorization to read and write State of Mind HealthKit samples import HealthKitUI func healthDataAccessRequest( store: HKHealthStore, shareTypes: Set<HKSampleType>, readTypes: Set<HKObjectType>? = nil, trigger: some Equatable, completion: @escaping (Result<Bool, any Error>) -> Void ) -> some View
-
6:26 - EmojiType
// EmojiType enum EmojiType: CaseIterable { case angry case sad case indifferent case satisfied case happy var emoji: String { switch self { case .angry: return "😡" case .sad: return "😢" case .indifferent: return "😐" case .satisfied: return "😌" case .happy: return "😊" } } }
-
6:32 - Create State of Mind sample for an event and emoji selection
/// Create State of Mind sample for an event and emoji selection func createSample(for event: EventModel, emojiType: EmojiType) -> HKStateOfMind { let kind: HKStateOfMind.Kind = .momentaryEmotion let valence: Double = emojiType.valence let label = emojiType.label let association = event.association return HKStateOfMind(date: event.endDate, kind: kind, valence: valence, labels: [label], associations: [association]) }
-
7:21 - Save State of Mind sample from emoji choice
// Save State of Mind sample from emoji choice func save(sample: HKSample, healthStore: HKHealthStore) async { do { try await healthStore.save(sample) } catch { // Handle error here. } }
-
10:34 - Query State of Mind samples
// Query State of Mind samples let datePredicate: NSPredicate = { ... } let associationsPredicate = NSCompoundPredicate ( orPredicateWithSubpredicates: associations.map { HKQuery.predicateForStatesOfMind(with: $0) } ) let compoundPredicate = NSCompoundPredicate( andPredicateWithSubpredicates: [datePredicate, associationsPredicate] ) let state0fMindPredicate = HKSamplePredicate.stateOfMind(compoundPredicate)
-
10:49 - Query State of Mind samples
// Query State of Mind samples let datePredicate: NSPredicate = { ... } let associationsPredicate = NSCompoundPredicate ( orPredicateWithSubpredicates: associations.map { HKQuery.predicateForStatesOfMind(with: $0) } ) let compoundPredicate = NSCompoundPredicate( andPredicateWithSubpredicates: [datePredicate, associationsPredicate] ) let stateOfMindPredicate = HKSamplePredicate.stateOfMind(compoundPredicate) let descriptor = HKSampleQueryDescriptor(predicates: [stateOfMindPredicate], sortDescriptors: []) var results: [HKStateOfMind] = [] do { // Launch the query and wait for the results. results = try await descriptor.result(for: healthStore) } catch { // Handle error here. }
-
10:54 - Query State of Mind samples (continued)
// Adjust each valence value to be from a range of 0.0 to 2.0. let adjustedValenceResults = results.map { $0.valence + 1.0 } // Calculate average valence. let totalAdjustedValence = adjustedValenceResults.reduce (0.0, +) let averageAdjustedValence = totalAdjustedValence / Double(results.count) // Convert valence to percentage. let adjustedValenceAsPercent = Int(100.0 * (averageAdjustedValence / 2.0))
-
11:33 - Query for relevant State of Mind samples with a specific label
// Query for relevant State of Mind samples with a specific label let label: HKStateOfMind.Label = .happy // Configure the query let datePredicate = HKQuery.predicateForSamples(withStart: dateInterval.start, end: dateInterval.end) let associationPredicate = HKQuery.predicateForStatesOfMind(with: association) let labelPredicate = HKQuery.predicateForStates0fMind(with: label) let compoundPredicate = NSCompoundPredicate( andPredicateWithSubpredicates: [datePredicate, associationPredicate, labelPredicate] ) let stateOfMindPredicate = HKSamplePredicate.stateOfMind(compoundPredicate) let descriptor = HKAnchoredObjectQueryDescriptor(predicates: [state0fMindPredicate], anchor: nil) // Fetch the results let results = descriptor.results(for: healthStore) let samples: [HKStateOfMind] = try await results.reduce([]) { $1.addedSamples }
-
11:45 - Process State of Mind sample data
// Process State of Mind sample data let happiestSample = samples.max { $0.valence < $1. valence } let happiestEvent: EventModel? = findClosestEvent(startDate: happiestSample?.startDate, endDate: happiestSample?.endDate)
-
-
찾고 계신 콘텐츠가 있나요? 위에 주제를 입력하고 원하는 내용을 바로 검색해 보세요.
쿼리를 제출하는 중에 오류가 발생했습니다. 인터넷 연결을 확인하고 다시 시도해 주세요.