Contents

preferredUnits(for:completion:)

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

Declaration

func preferredUnits(for quantityTypes: Set<HKQuantityType>, completion: @escaping  @Sendable ([HKQuantityType : HKUnit], (any Error)?) -> Void)
func preferredUnits(for quantityTypes: Set<HKQuantityType>) async throws -> [HKQuantityType : HKUnit]

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 bodyMass 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 bloodGlucose results identifier for an exception). You should also observe the HKUserPreferencesDidChange notification, and update the user interface whenever the user changes his or her preferred units.

See Also

Accessing the preferred units