我发现健康应用程序有一个专注力部分,但我没有在文档中找到如何对该部分做出贡献。为这一要求编制一个网络搜索会给我一个全球务虚会的集合.你能告诉我一些有意义的事情吗?
发布于 2016-08-11 14:42:56
我找到了HKCategoryTypeIdentifierMindfulSession。
用于记录留心会话的类别示例类型。
这里是iOS 10+。我认为他们创造了这个新的呼吸应用程序,以跟踪多少时间你花了冥想。这是很棒的,你应该使用这个,如果你建立冥想应用程序或其他在这一领域。
发布于 2017-02-05 06:33:40
Swift 3.1,Xcode 8.2
下面是关于healthkit的mindfull部分的方法
但在融合之前要记住一点:-
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")
}
}
}不要忘记在plist中添加隐私选项。
就像这样
隐私-健康共享使用说明
隐私-健康更新使用说明

然后将数据保存到health部分
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
}
})
}
}这里我使用了一个简单的计时器,它表示用户冥想的时间,当用户开始它开始分析的时间,然后当它停止时,保存在health部分中的数据
git集线器上的完整项目链接,以供初学者参考- 下载
希望能帮上忙
发布于 2016-09-30 15:16:04
您可以通过这样的方式在HealthKit上编写一个留心的分钟会话:
创建健康商店:
HKHealthStore *healthStore = [[HKHealthStore alloc] init];请用户获得正确的权限:
NSArray *writeTypes = @[[HKSampleType categoryTypeForIdentifier:HKCategoryTypeIdentifierMindfulSession]];
[healthStore requestAuthorizationToShareTypes:[NSSet setWithArray:writeTypes] readTypes:nil completion:^(BOOL success, NSError * _Nullable error) {
//manage the success or failure case
}];编写一个函数来实际保存会话:
-(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);
}
}];
}编辑:,请注意:此功能仅在iOS 10上可用。如果您要求在iOS 9(或更少)上获得此权限,应用程序将崩溃。
https://stackoverflow.com/questions/38898162
复制相似问题