我发现“健康”应用程序具有“正念”部分,但我在文档中没有找到如何为该部分做出贡献。通过网络搜索满足这一要求,我得到了世界范围内撤退中心的集合……您能指导我一些有意义的事情吗?
Swift 3.1,Xcode 8.2
这是关于healthkit的
完整说明部分,但在集成之前请记住以下几点:
-1-仅在ios 10及更高版本中可用
2-您需要获得用户许可才能访问这些数据
3-在这里,我正在展示如何填充仅根据问题需要,在“保健工具包正念”部分中的数据
首先获得用户许可,
假设我们已经在按钮中实现了IBAction
//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")
}
}
}
Run Code Online (Sandbox Code Playgroud)
不要忘记在plist中添加隐私选项
这是这样的
隐私-健康分享使用说明
隐私-健康更新使用说明
然后将数据保存到Health Kit注意事项部分
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
}
})
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,我使用了一个简单的计时器,它表示用户冥想的时间,即用户开始分析时的时间,然后在停止时将其保存在Health Kit注意事项部分的数据
git hub上的完整项目链接,供初学者参考- 下载
希望对您有所帮助
| 归档时间: |
|
| 查看次数: |
1873 次 |
| 最近记录: |