WWDC Quick Look 💓 By SwiftGGTeam
Meet Nearby Interaction

Meet Nearby Interaction

Watch original video

Highlight

iOS 14 introducedNearbyInteractionThe framework uses the Ultra Wideband technology of the Apple U1 chip to achieve precise positioning between devices.

Core Content

AirDrop has already demonstrated a spatial awareness scene in iOS 13. Users point their iPhone at another person, and the sharing panel brings more relevant recipients to the top. This experience relied on the Apple U1 chip in iPhone 11, but at the time developers couldn’t put the same capabilities directly into their apps.

Nearby Interaction in iOS 14 opens this up. Two devices that agree to participate can establish a session and continuously receive each other’s distance and direction. Distance is in meters and direction is a three-dimensional unit vector relative to the host machine. Applications can integrate these updates into the interface, game rules, or matching process to create interactions that are closer to the real space.

This capability brings privacy boundaries from the first step. The system will display permission prompts to both users; the authorization is one-time and lasts until the app is exited. Devices also exchange discovery tokens generated by the system. The token is only valid in the current session and will also become invalid after the session expires or the user exits the app.

There are three real tasks that developers have to deal with: building one for each peerNISession, exchange the discovery token through its own network layer, and then useNINearbyPeerConfigurationStart ranging and heading updates. Then you have to deal with realistic constraints such as pause, resume, invalidation, hardware support, field of view, occlusion and device orientation.

Detailed Content

Create a Nearby Interaction session

(03:29) The basic unit of Nearby Interaction is session. Each session corresponds to a peer. App is created firstNISession, set the delegate, and then send the local discovery token to the other party through its own network layer. The other party has to do the same thing.

(05:25) discovery token followNSSecureCoding, so you can useNSKeyedArchiverEncoded and then transmitted through MultipeerConnectivity, cloud channels, or the app’s existing network layer. After receiving the other party’s token, the App decodesNIDiscoveryToken,createNINearbyPeerConfiguration, finally callrun

// A session instance. Store in whichever data structure makes the most sense for your app.
var niSession: NISession?

// Instantiate a new session object and set the session's delegate.
func prepareMySession() {
  // Verify hardware support.
  guard NISession.isSupported else {
    print("Nearby Interaction is not available on this device.")
    return
  }

  // Create a new session for each peer.
  niSession = NISession()

  // Set the session’s delegate.
  niSession?.delegate = self // This class of 'self' needs to conform to NISessionDelegate.
}

// Share the encoded discovery token to the peer you intend to interact with.
func sendDiscoveryTokenToMyPeer(myPeer: Any /* change to whichever type represents peers in your app */) {
	guard let myToken = niSession?.discoveryToken else {
		// The session object is not initialized or has been invalidated.
		return
	}

	if let encodedToken = try? NSKeyedArchiver.archivedData(withRootObject: myToken, requiringSecureCoding: true) {
		<# share token using your app's networking layer #>
	}
}

// Once you receive a token from the peer, create a configuration and run the session.
// This functions shows how to decode token data that was previously encoded using NSKeyedArchiver.
func runMySession(peerTokenData: Data) {
  guard let peerDiscoveryToken = try? NSKeyedUnarchiver.unarchivedObject(ofClass: NIDiscoveryToken.self, from: peerTokenData) else {
    print("Unexpectedly failed to decode discovery token.")
    return
  }

  // Create a session configuration using the discovery token received from the peer.
  let config = NINearbyPeerConfiguration(peerToken: peerDiscoveryToken)

  // Run the session with the configuration.
  niSession?.run(config)
}

Key points:

  • NISession.isSupportedIt’s an entrance check. Provide an alternative experience when U1 support is not available. -NISession()Resources will be allocated for the current session and corresponding discovery tokens will be generated. -delegateReceive system callbacks, including nearby object updates and session life cycle events.
  • The discovery token comes from the current session and can only be used within the validity period of this session. -NINearbyPeerConfiguration(peerToken:)Put the other party’s token into the configuration. -runStart a session, after which the delegate starts receiving updates from nearby objects.

Handling updates, removals and lifecycle

(08:26) After starting the session, the App passesNISessionDelegateReceive nearby object updates.didUpdateThe callback will bring nearby objects; there are three properties on the object: discovery token, distance and direction. The discovery token is used to map updates back to users or peers in the business.

(08:37) The system will also notify the App when it will no longer interact with a nearby object. The reason may betimeout, for example, the device is too far away and there is no activity for a period of time; it may also bepeerEnded, indicating that the other end has clearly invalidated the session. speech reminder,peerEndedThis is a best-effort notification, and the app cannot assume it will arrive.

(09:21) The session life cycle also includes suspension and expiration. The session will be suspended when the app enters the background. After the pause, the session will not automatically resume; if you want to continue the interaction, both parties need to call it again.run, but there is no need to re-exchange discovery tokens. After the session is invalidated, it cannot be run again, and the related tokens cannot be used again; to resume interaction, a new session must be created and the tokens must be exchanged again.

Key points:

  • didUpdateIt is the main callback that drives the UI and uses discovery token to associate with peers. -timeoutIt means that there is no session activity for a period of time. The common reason is that the device is too far away. -peerEndedIt means that the other party has taken the initiative to terminate, but this notice is not guaranteed to be delivered.
  • You can restart after suspension endsrun, no need to re-exchange tokens.
  • Invalidated is the termination status. You can only create a new session and re-exchange tokens.

One peer corresponds to one session

(10:38) Nearby Interaction does not limit the App to interact with only one device. Each device can run multiple sessions at the same time, but the recommended principle is clear: one session per peer.

This model makes multi-device scenarios easier to manage. App can reuse delegate or other network layer code, but the session itself must be stored in a data structure such as a dictionary, and mapped to the corresponding peer identifier in the business.NISession. When a peer disconnects, times out, or fails, the App can only process that session.

Key points:

  • Multi-person space interaction uses multiple point-to-point sessions instead of a single shared session.
  • Dictionary mapping can combine peer users, discovery tokens andNISessionGet in touch.
  • When reusing a delegate, the corresponding peer must also be found from the callback.
  • This structure is suitable for multiplayer gaming, close collaboration and multi-device pairing.

Understand the distance and direction of nearby objects

(11:39) Each nearby object has three properties. The discovery token identifies the object; distance represents the metric distance between the two devices; direction is a three-dimensional unit vector representing the direction of the other device relative to the local machine.

(12:28) distance and direction are both nullable. Fields that are nil are normal measurement results, usually derived from real-life conditions such as hardware field of view, orientation, and occlusion. The UI must be able to express three states: “only distance”, “both distance and direction”, and “no measurement yet”.

Key points:

  • distance can directly support close, far, distance threshold and progressive UI.
  • direction is suitable for arrows, pointing prompts and face-to-face interactions.
  • Both fields may be nil, business logic cannot force unpacking.
  • The discovery token is the key to linking the measurement results back to the user’s identity.

Check hardware support first

(12:40) Nearby Interaction relies on U1-equipped devices running iOS 14. The talk recommends checking for hardware support first at strategic places in your app, and falling back to another experience if it’s not supported.

// Always verify hardware support.
guard NISession.isSupported else {
  print("Nearby Interaction is not available on this device.")
  return
}

Key points:

  • NISession.isSupportedIt’s a framework-level capability judgment, don’t just guess based on the device model.
  • The fallback experience should be designed in advance during the product process, such as normal matching codes, list selection, or web-based invitations.
  • This check is also suitable to be placed at the function entrance to decide whether to display space interaction capabilities.

Design the degraded experience of field of view, orientation and occlusion

(12:49) Nearby Interaction has a directional field of view. The presentation describes it as a cone extending from the back of the phone, roughly corresponding to the iPhone 11’s Ultra Wide camera field of view. When the other party’s device is within this field of view, distance and direction are more likely to be generated with high confidence; when it is outside the field of view, the App may only get the distance but not the direction.

(13:36) Device orientation also affects measurements. The best situation is when both parties hold it in portrait orientation. One vertical screen and one horizontal screen will lead to a decrease in measurement usability. The talk also calls for attention to occlusion: clear lines of sight between devices work best, and measurement usability is reduced when walls, people or other objects get in the way.

(14:35) The development process can take advantage of Xcode’s Simulator support. The same set of App code can trigger distance and direction updates between two or more Simulator windows, making it possible to debug basic processes without multiple physical devices.

Key points:

  • The direction capability is affected by the field of view, and direction cannot be regarded as an always available input.
  • The UI should guide the user to hold the device in portrait orientation.
  • Occlusion reduces usability, it is better to give the user clear prompts to redirect or approach.
  • Simulator supports session, token, delegate and UI state machines suitable for verification.

Core Takeaways

  • Make an interface to find people at close range: In an event, classroom or office scene, let two paired devices approach each other through distance and direction. This capability comes from Nearby Interaction’s continuous relative position updates. When implementing, first exchange discovery tokens, and thendidUpdatedistance and direction in are mapped to distance literals and direction arrows.

  • Make a collaborative entrance that is unlocked face to face: Two people need to stand close enough and align in the direction before opening a shared whiteboard, two-player game or partner task. The session provides distance and direction, and the App can define its own thresholds. Use at the beginningNISession.isSupportedFilter the device and establish a session for each peer.

  • Make a multi-device space game: Each player’s mobile phone and other players run sessions separately, and the App uses a dictionary to save peer toNISessionof mapping. This can handle a player’s timeout, peerEnded or invalidated independently without affecting other connections.

  • Make a privacy-friendly device pairing process: Put the discovery token exchange after the action that the user explicitly consents to, and clean up the state when the app exits or the session expires. This design fits the one-time permission and token life cycle in the speech, and is suitable for accessory configuration, temporary collaboration and on-site check-in.

  • Make a space UI with a downgrade prompt: display the distance bar when only distance is received, and then display the arrow when direction is also available; when the field is nil, prompt the user to turn to vertical screen, remove the cover, or realign. This function directly corresponds to the speech’s description of nullable distance/direction, field of view, orientation and occlusion.

Comments

GitHub Issues · utterances