Returns a sample containing a numeric measurement with the provided device and metadata.
SDKs
- iOS 9.0+
- Mac Catalyst 13.0+
- watchOS 2.0+
Framework
- Health
Kit
Declaration
+ (instancetype)quantitySampleWithType:(HKQuantity Type *)quantityType quantity:(HKQuantity *)quantity startDate:(NSDate *)startDate endDate:(NSDate *)endDate device:(HKDevice *)device metadata:(NSDictionary<NSString *,id> *)metadata;
Parameters
quantityType
The type of sample to be created. HealthKit defines a number of different quantity types, representing different types of health and fitness data. For the complete list of quantity type identifiers, see
HKQuantity
.Type Identifier quantity
The value to be stored in the sample. The quantity object must use units that are compatible with the provided quantity type. If the units are not compatible, this method throws an exception (
NSInvalid
).Argument Exception startDate
The start date for the sample. This date must be equal to or earlier than the end date; otherwise, this method throws an exception (
NSInvalid
).Argument Exception endDate
The end date for the sample. This date must be equal to or later than the start date; otherwise, this method throws an exception (
NSInvalid
).Argument Exception device
The device that generated the data for this sample.
metadata
The metadata dictionary contains extra information describing this sample. The dictionary’s keys are all
NSString
objects. The values may beNSString
objects,NSNumber
objects, orNSDate
objects. For a complete list of predefined metadata keys, see Metadata Keys.Using predefined keys helps facilitate sharing data between apps; however, you are also encouraged to create your own, custom keys as needed to extend the HealthKit quantity sample’s capabilities.
Return Value
A valid quantity sample with the device and metadata.
Discussion
HealthKit uses quantity samples to represent sample data using a single numeric value. To create a quantity sample, first create the corresponding quantity type and quantity, and then set its start date, end date, device, and metadata. You produce a new quantity sample with the provided device and metadata.
HKDevice *device = [[HKDevice alloc] initWithName:deviceName
manufacturer:manufacturerName
model:modelName
hardwareVersion:hardwareVersionNumber
firmwareVersion:firmwareVersionNumber
softwareVersion:softwareVersionNumber
localIdentifier:localIdentifier
UDIDeviceIdentifier:deviceIdentifier];
NSDictionary *metadata =
@{HKMetadataKeyDigitalSignature:digitalSignature,
HKMetadataKeyTimeZone:timeZone};
HKQuantityType *quantityType =
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];
HKUnit *bpm = [HKUnit unitFromString:@"count/min"];
HKQuantity *quantity = [HKQuantity quantityWithUnit:bpm
doubleValue:72.0];
HKQuantitySample *sample =
[HKQuantitySample quantitySampleWithType:quantityType
quantity:quantity
startDate:start
endDate:end
device:device
metadata:metadata];