填充 iOS 健康部分的正念部分

Populating the mindfulness section in the iOS Health Section

我发现健康应用程序有一个正念部分,但我没有在文档中找到如何为该部分做出贡献。为这个要求编造一个网络搜索 returns 我收集了世界各地的静修中心......你能告诉我一些有意义的事情吗?

我找到了 HKCategoryTypeIdentifierMindfulSession

A category sample type for recording a mindful session.

Apple docs.

这只是 iOS 10+。我认为他们为新的 Breathe 应用程序创建了这个来跟踪你花了多少时间冥想。太棒了,如果你在这个领域构建冥想应用程序或其他东西,你应该使用它。

你可以这样在 HealthKit 上写一个正念时刻 session:

创建健康商店:

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
    }];

写一个函数实际保存 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);
        }
    }];
}

编辑: 请注意:此功能仅在 iOS 10 上可用。如果您在 iOS 9(或更少)应用程序将崩溃。

Swift3.1,Xcode8.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")
        }
    }
}

Dont forgot to add privacy option in plist

是这样的
隐私 - Health Share 使用说明
隐私 - 健康更新使用说明

然后将数据保存到 Health Kit mindfull 部分

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 kit mindfull 部分

完整项目 link 在 git 中心供初学者参考 - Download

希望对您有所帮助