Instance Method

preferredUnitsForQuantityTypes:completion:

Returns the user’s preferred units for the given quantity types.

Declaration

- (void)preferredUnitsForQuantityTypes:(NSSet<HKQuantityType *> *)quantityTypes completion:(void (^)(NSDictionary<HKQuantityType *,HKUnit *> *preferredUnits, NSError *error))completion;

Parameters

quantityTypes

A set of HKQuantityType identifiers. These identifiers represent the quantity types to be examined. Before calling this method, your app must request read or share access to all the types in this set.

completion

A block that this method calls as soon as it finishes looking up the preferred units. This block is passed the following parameters:

preferredUnits

If the lookup is successful, this parameter contains a dictionary with HKQuantityType identifiers for the keys and HKUnit objects for the values. The keys match those passed to the quantityTypes parameter. If an error occurs, this parameter is set to nil.

error

An error object. This method returns an error if the preferred units are inaccessible or if your app has not yet requested permission to access the quantity types; otherwise, this parameter is set to nil.

Discussion

This method runs asynchronously. As soon as it finishes looking up the preferred units, it calls the completion block on an anonymous background queue.

By default, the preferred units are based on the device’s current locale. For example, in the US, the preferred units for the HKQuantityTypeIdentifierBodyMass identifier are pounds. Other regions may use kilograms or stones. However, users can change their preferred units in the Health app at any time.

Your app should present HealthKit data using the current preferred units (see the HKQuantityTypeIdentifierBloodGlucose results identifier for an exception). You should also observe the HKUserPreferencesDidChangeNotification notification, and update the user interface whenever the user changes his or her preferred units.

See Also

Accessing the Preferred Units

HKUserPreferencesDidChangeNotification

Notifies observers whenever the user changes his or her preferred units.