iOS HealthKit how to save Heart Rate (bpm) values? Swift

前端 未结 3 856
终归单人心
终归单人心 2021-02-13 21:49

How to use : HKUnit

Sample type Unit type Unit name Unit string Heart Rate count/time Beats per Minute \"count/min”

3条回答
  •  囚心锁ツ
    2021-02-13 22:00

    In ObjectiveC (for those not using Swift, expanded for easy reading):

    #define nilable __nullable
    #define kCountPerMinUnitStr @"count/min"
    
    typedef void (^VoidBoolErrorBlock) (BOOL        success,
                                        NSError *   nilable error);
    
    enum {
        // Memory Constants
        kNil       = 0,
        kNilUPP    = 0,
        kNilRefCon = 0
    };
    
    
    /**
     writes a weight sample to HealthKit
    
     @param weight as HKQuantityTypeIdentifierBodyMass
     */
    - (void) writeCDAHeartRateSample: ( NSStringPtr ) valueStr
                            cdaUnits: ( NSStringPtr ) cdaUnits
                                date: ( NSDate * ) date {      // was (CGFloat)
        double  theValue = kNil;
    
        // Each quantity consists of a value and a unit.
        HKUnit *            theMeasurementUnit = nil;
        HKQuantity *        theQuantity        = nil;
    
        HKQuantityType *    theHeartRateType   = nil;
        NSDate *            theDate            = nil;
    
        // For every sample, we need a sample type, quantity and a date.
        HKQuantitySample *  theSample = nil;
        VoidBoolErrorBlock  theBlock  = nil;
    
        theValue           = [ valueStr integerValue ];
    //    theMeasurementUnit = [ HKUnit unitFromCDAString: cdaUnits ];
        theMeasurementUnit = [ HKUnit unitFromString: kCountPerMinUnitStr ];    /// FIXME: temp code StackOverflow example
        theQuantity        = [ HKQuantity quantityWithUnit: theMeasurementUnit
                                               doubleValue: theValue ];
    
        theHeartRateType = [ HKQuantityType quantityTypeForIdentifier: HKQuantityTypeIdentifierHeartRate ];
        theDate          = ( date != nil ) ? date : [ NSDate date ];///???: default to today's date?
    
        theBlock = [ self saveCDAHeartRateObjectBlock: theValue ];
    
        // For every sample, we need a sample type, quantity and a date.
        theSample = [HKQuantitySample quantitySampleWithType: theHeartRateType
                                                          quantity: theQuantity
                                                         startDate: theDate
                                                           endDate: theDate];
        [ self.healthStore saveObject: theSample
                       withCompletion: theBlock ];
    }
    
    
    
    /**
     companion block encapsulation for writeCDAHeartRateSample{}
    
     @return  completion block for saveObject: withCompletion in writeCDAHeartRateSample{}
     */
    - ( VoidBoolErrorBlock ) saveCDAHeartRateObjectBlock: ( double ) value {
        VoidBoolErrorBlock theResult = nil;
    
        theResult = ^(BOOL success, NSError *error) {
            if (!success) {
                ///NSLog(@"Error while saving Heart Rate (%f) to Health Store: %@.", value, error);
            }
        };
    
        return theResult;
    }
    

提交回复
热议问题