问题
I found the Health app has a mindfulness section, yet I did not find in the documentation how to contribute to that section. Concocting a web search for this requirement returns me a collection of worldwide retreat centers... could you direct me to something meaningful?
回答1:
I found HKCategoryTypeIdentifierMindfulSession
.
A category sample type for recording a mindful session.
Apple docs.
This is iOS 10+ only. I think they created this for the new Breathe app to track how much time you spent meditating. It is awesome, you should use this if you building meditation app or something in this area.
回答2:
Swift 3.1, Xcode 8.2
Here is the way to mindfull section on healthkit
but before integration keep some point in mind:-
1 - it's only available in ios 10 and onwards
2 - you need to take user permission to access those data
3 - here I am showing how to populate the data in health kit mindfulness section only as per question need
First take user permission
let say we have taken implemented an IBAction in a button
//Taking permission from user
@IBAction func activateHealthKit(_ sender: Any) {
let typestoRead = Set([
HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.mindfulSession)!
])
let typestoShare = Set([
HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.mindfulSession)!
])
self.healthStore.requestAuthorization(toShare: typestoShare, read: typestoRead) { (success, error) -> Void in
if success == false {
print("solve this error\(error)")
NSLog(" Display not allowed")
}
if success == true {
print("dont worry everything is good\(success)")
NSLog(" Integrated SuccessFully")
}
}
}
Dont forgot to add privacy option in plist
which goes something like this
Privacy - Health Share Usage Description
Privacy - Health Update Usage Description
then save the data to health kit mindfull section
func saveMindfullAnalysis() {
// alarmTime and endTime are NSDate objects
if let mindfulType = HKObjectType.categoryType(forIdentifier: .mindfulSession) {
// we create our new object we want to push in Health app
let mindfullSample = HKCategorySample(type:mindfulType, value: 0, start: self.alarmTime, end: self.endTime)
// at the end, we save it
healthStore.save(mindfullSample, withCompletion: { (success, error) -> Void in
if error != nil {
// something happened
return
}
if success {
print("My new data was saved in HealthKit")
} else {
// something happened again
}
})
}
}
here I have taken a simple timer which denotes the user meditation time when user start the time it's started analyzing and then when stop it save the data on health kit mindfull section
full project link on git hub for reference to the beginner's - Download
Hope it is helpful
回答3:
You can write a Mindful Minutes session on HealthKit this way:
Create the Health store:
HKHealthStore *healthStore = [[HKHealthStore alloc] init];
Ask the user for the right permission:
NSArray *writeTypes = @[[HKSampleType categoryTypeForIdentifier:HKCategoryTypeIdentifierMindfulSession]];
[healthStore requestAuthorizationToShareTypes:[NSSet setWithArray:writeTypes] readTypes:nil completion:^(BOOL success, NSError * _Nullable error) {
//manage the success or failure case
}];
Write a function to actually save the session:
-(void)writeMindfulSessionWithStartTime:(NSDate *)start andEndTime:(NSDate *)end{
HKCategoryType *mindfulType = [HKCategoryType categoryTypeForIdentifier:HKCategoryTypeIdentifierMindfulSession];
HKCategorySample* mindfulSample = [HKCategorySample categorySampleWithType:mindfulType value:0 startDate:start endDate:end];
[healthStore saveObject:mindfulSample withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"Error while saving a mindful session: %@.", error);
}
}];
}
EDIT: Please, pay attention: this feature is only available on iOS 10. If you ask for this permission on iOS 9 (or less) the app will crash.
来源:https://stackoverflow.com/questions/38898162/populating-the-mindfulness-section-in-the-ios-health-section