Contents

sendToRemoteWorkoutSession(data:completion:)

Sends the provided data to the remote workout session.

Declaration

func sendToRemoteWorkoutSession(data: Data, completion: @escaping  @Sendable (Bool, (any Error)?) -> Void)
func sendToRemoteWorkoutSession(data: Data) async throws

Parameters

  • data:

    A data object that contains the data your app is sending to the remote workout session.

  • completion:

    A block that the system calls when the send attempt is complete. The system passes the following parameters:

    success

    A Boolean value that indicates whether the system successfully sent the data.

    error

    If sucess is False, this contains an object that describes the error. Otherwise, it’s nil.

Discussion

Call this method to send data to your remote workout session. You can send data from either the HKWorkoutSessionType.mirrored or HKWorkoutSessionType.primary session.

let archivedData = try? NSKeyedArchiver.archivedData(withRootObject: data, requiringSecureCoding: true)
guard let archivedData = archivedData, !archivedData.isEmpty else {
    // Handle the error here.
    fatalError("*** Encoded data is empty ***")
}

// Send the data to the companion iPhone.
Task {
    do {
        try await session.sendToRemoteWorkoutSession(data: archivedData)
    }
    catch {
        // Handle the error here.
        fatalError("*** An error occurred while sending the health data to the companion iPhone: \(error.localizedDescription) ***")
    }
}

See Also

Working with remote workout sessions