Integrating Nearby Interaction (Apple UWB) into iOS App
Nearby Interaction — Apple framework for precise distance and direction measurement to another iPhone or accessory with U1/U2 chip. Not Bluetooth proximity, not iBeacon, not GPS. UWB (Ultra Wideband) works at centimeter accuracy: app receives distance in meters with ±10–15 cm precision and direction — 3D vector to device in space (on iPhone 14 Pro+ via dual-directional antennas).
Used for: precise aiming in file sharing and payments, finding lost items (like AirTag), smart home control with spatial binding, AR apps with real spatial anchoring.
Limitations to Know Before Start
iPhone 11+ only. U1 chip first appeared in iPhone 11. iPhone SE (all generations) — no UWB. iPad — no UWB (except specialized configs). App must check NISession.deviceCapabilities.supportsPreciseDistanceMeasurement.
Foreground only. NISession works when app active. No exceptions. Measurement in background — UWB unsuitable, hardware limitation.
Two devices, one session. For P2P measurement both devices must create NISession and exchange NIDiscoveryToken. Token can't be shared beforehand — generated on session creation and changes on restart. Token exchange via MultipeerConnectivity, Bluetooth, server — your choice.
How Session Works
import NearbyInteraction
import MultipeerConnectivity
class UWBSessionManager: NSObject, NISessionDelegate {
private var niSession = NISession()
private var peerToken: NIDiscoveryToken?
override init() {
super.init()
niSession.delegate = self
}
func startSession(with peerToken: NIDiscoveryToken) {
let config = NINearbyPeerConfiguration(peerToken: peerToken)
config.isCameraAssistanceEnabled = true // Camera Assistance on iPhone 14 Pro+
niSession.run(config)
}
func session(_ session: NISession,
didUpdate nearbyObjects: [NINearbyObject]) {
guard let peer = nearbyObjects.first else { return }
if let distance = peer.distance {
print("Distance: \(distance) m")
}
if let direction = peer.direction {
print("Direction: \(direction)") // simd_float3
}
}
func session(_ session: NISession,
didInvalidateWith error: Error) {
// NIErrorCode.userDidNotAllow, .resourceUsageLimitReached, .sessionFailed
// on .resourceUsageLimitReached — just restart session
}
}
Token Exchange via MultipeerConnectivity
Most common approach for P2P: use MCSession for token transfer, then start NISession. Important: NIDiscoveryToken can't pass as string — it's Codable, encode via NSKeyedArchiver:
let tokenData = try NSKeyedArchiver.archivedData(
withRootObject: niSession.discoveryToken!,
requiringSecureCoding: true
)
mcSession.send(tokenData, toPeers: peers, with: .reliable)
Receiving side:
let token = try NSKeyedUnarchiver.unarchivedObject(
ofClass: NIDiscoveryToken.self,
from: data
)
JSON encoding attempt — won't work, NIDiscoveryToken not standard Encodable.
Camera Assistance
iPhone 14 Pro and 15 series support isCameraAssistanceEnabled = true. System shows native AR overlay helping aim at device. User sees direction arrow. Activates only with camera permission. Adds ~200 ms latency before first direction data.
Work Included
- Device compatibility check (
NIDeviceCapability) -
NISessionsetup with P2P or accessory config (NINearbyAccessoryConfiguration) -
NIDiscoveryTokenexchange mechanism (MultipeerConnectivity, Bluetooth, REST) - Real-time distance/direction update handling
- Camera Assistance overlay
- Error handling and session restart
- Testing on two physical devices (simulator UWB unsupported)
Timeline
5 days. Testing needs two real iPhones with U1/U2 — simulator insufficient. Cost calculated individually after analyzing use case.







