rn-watch-connectv2.0

User Info

Queue dictionaries for background delivery. Every one arrives, in order, even when the Watch isn't reachable.

A User Info is a dictionary queued for background delivery. Unlike Application Context, every User Info is delivered, in order, even when the Watch isn't reachable. Use it when every value matters, such as each completed workout.

Each User Info you send is a Transfer until it's delivered:

Inbound User Info from the Watch arrives through onUserInfoReceived.

A dictionary's values must be ones WatchConnectivity can carry: strings, numbers, booleans, arrays and nested objects.

Transfer ids last for one app run

After your app restarts, a Transfer that's still outstanding gets a new id. Read outstandingUserInfoTransfers at launch to pick them up. Their finish events and cancelUserInfoTransfer use the new ids.

transferUserInfo

transferUserInfo<T extends Record<string, any>>(userInfo: T): OutstandingUserInfoTransfer

Queues a User Info for delivery to the Watch and returns at once. The Watch app receives it in session(_:didReceiveUserInfo:).

Type parameters

NameDescription
TThe shape of the User Info you send.

Parameters

NameTypeDescription
userInfoTThe User Info to queue.

Returns the Transfer snapshot, in the same shape as outstandingUserInfoTransfers:

FieldTypeDescription
idstringThe Transfer id, valid for the current app run.
userInfoRecord<string, any>The User Info being sent.
isTransferringbooleanWhether the Transfer is in progress right now.

The type is OutstandingUserInfoTransfer. The older name UserInfoTransfer is a deprecated alias of it.

Throws (synchronously):

CodeWhen
ERR_UNSUPPORTED_PLATFORMCalled on a platform other than iOS.

Delivery failures don't throw here: they arrive later, in onUserInfoTransferFinished.

Example

import  from "rn-watch-connect";

const transfer = .({ : "run", : 5.2 });
const transfer: OutstandingUserInfoTransfer
.("Queued Transfer", .);

cancelUserInfoTransfer

cancelUserInfoTransfer(transferId: string): { id: string }

Cancels an outstanding User Info Transfer. Its onUserInfoTransferFinished event is sent immediately, with error.code "ERR_TRANSFER_CANCELLED".

Parameters

NameTypeDescription
transferIdstringThe id of an outstanding Transfer, from the current app run.

Returns { id: string }: the id of the cancelled Transfer.

Throws (synchronously):

CodeWhen
ERR_INVALID_TRANSFER_IDtransferId isn't an outstanding Transfer: it was already delivered, already cancelled, or is from an earlier app run.
ERR_UNSUPPORTED_PLATFORMCalled on a platform other than iOS.

Its error message starts with Expo's prefix, "Calling the 'cancelUserInfoTransfer' function has failed", so match on code.

If the Transfer was delivered just before you cancel, the cancel throws ERR_INVALID_TRANSFER_ID and the finish event reports the delivery, with error: null.

Example

import , { type  } from "rn-watch-connect";

const  = .({ : true });

try {
  .(.);
} catch () {
  const  = ( as { ?:  }).;
  if ( === "ERR_INVALID_TRANSFER_ID") {
    // Already delivered or cancelled: nothing to do.
  }
}

outstandingUserInfoTransfers

readonly outstandingUserInfoTransfers: OutstandingUserInfoTransfer[]

The User Info Transfers that haven't been delivered yet, as Transfer snapshots (id, userInfo, isTransferring). It's [] when there are none, and on platforms other than iOS.

Example

import  from "rn-watch-connect";

// At launch: pick up Transfers from the previous run, with their new ids.
for (const  of .) {
  .("Still queued:", ., .);
}

onUserInfoReceived

onUserInfoReceived: (userInfo: Record<string, any>) => void

Fires when a User Info arrives from the Watch. The listener gets the dictionary itself. User Info arrives in the order the Watch sent it.

Example

import {  } from "expo";
import  from "rn-watch-connect";

export function () {
  (, "onUserInfoReceived", () => {
    .("Workout from the Watch:", .workout);
  });
  return null;
}

onUserInfoTransferFinished

onUserInfoTransferFinished: (event: UserInfoTransferFinishedEvent) => void

Fires exactly once per User Info Transfer, however it ends. The event is the Transfer snapshot plus error:

FieldTypeDescription
idstringThe Transfer id.
userInfoRecord<string, any>The User Info that was sent.
isTransferringbooleanWhether the Transfer was in progress.
error{ message: string; code: WatchConnectErrorCode } | nullHow it ended (see below).
How it endederror
Delivered to the Watchnull
Failed{ message, code } from WatchConnectivity, for example code: "ERR_DELIVERY_FAILED"
Cancelled with cancelUserInfoTransfer{ message, code: "ERR_TRANSFER_CANCELLED" }, sent immediately

If your Watch app is uninstalled during a Transfer, the Transfer stays outstanding and no finish event is sent, even after a reinstall. Cancel it to end it, for example when onWatchAppInstallChanged reports isWatchAppInstalled: false.

Example

import {  } from "expo";
import  from "rn-watch-connect";

export function () {
  (, "onUserInfoTransferFinished", () => {
    if (.?. === "ERR_TRANSFER_CANCELLED") {
      .("Cancelled:", .);
    } else if (.) {
      .("Failed:", ., ..);
    } else {
      .("Delivered:", .);
    }
  });
  return null;
}

On this page