Jad
Jad

Reputation: 2238

Health Kit WatchOs 3: How do I record a mindfulSession?

How do I record a mindfulSession while using HKWorkoutSession in watchOS 3? I'm able to ask for mindfulSession permissions both reading and writing but when I use store.execute(query) to start the HKWorkoutSession I don't get anything in the updateHanlder. The heart rate with HKWorkoutSession works perfectly even in background. but I'm not able to figure out how suppose this to work. HKCategoryTypeIdentifier.mindfulSession

Any Help will be appreciated.

Thanks,

Upvotes: 3

Views: 613

Answers (1)

Jad
Jad

Reputation: 2238

Swift 3: Watch OS 3: Well, I figured it out. HKCategoryTypeIdentifier.mindfulSession is used as a category type to save mindfulness sessions to health store. I actually use it on watchOS 3 to store meditations sessions. as the following;

// Create Mindfulness category type
let categoryType = HKSampleType.categoryType(forIdentifier: HKCategoryTypeIdentifier.mindfulSession)

// Now create the sample
let smpleObject = HKCategorySample(type: sampleType!, value: HKCategoryValue.notApplicable.rawValue, start: Date().addingTimeInterval(-10000), end: Date())


// Finally save to health store
store.save(smpleObject) { (result:Bool, error:Error?) in

        if result{

            print("Saved")
        }else{

           print("error saving mindfulSession",error?.localizedDescription)
        }
}

Now if you open Health App on the iPhone you should see some data in the mindfulness section.

Upvotes: 6

Related Questions