Contents

DevicePicker

A SwiftUI view that displays other devices on the network, and creates an encrypted connection to a copy of your app running on that device.

Declaration

@MainActor @preconcurrency struct DevicePicker<Label, Fallback> where Label : View, Fallback : View

Mentioned in

Overview

Always display the picker as a full-screen, modal view. If the user selects a device, the system calls the closure you passed as the onSelect parameter. If the user cancels the picker, it silently closes.

DevicePicker(
    .applicationService(name: "MyAppService")) { endpoint in
        myDeviceManager.connectTo(endpoint: endpoint)
    } label: {
        Text("Connect to a local device.")
    } fallback: {
        Text("Not supported.")
    } parameters: {
        // This example uses the default application services parameters;
        // however, you can add a NWProtocolFramer to provide application-level
        // messaging.
        .applicationService
    }

If the current device doesn’t support device discovery, the system displays the fallback view instead of the device picker. Use the DevicePickerSupportedAction environment value to check whether the current device supports device discovery.

struct SettingsView: View {

    @Environment{\.devicePickerSupports} var myDevicePickerSupports
    @Binding var showDevicePicker: Bool

    var body: some View {
        if myDevicePickerSupports(.applicationService("MyAppService"),
                                  parameters: { .applicationService }) {
            Button("Select A Device") {
                // Display a device picker.
                showDevicePicker = true
            }
        }
    }
}

Topics

Creating a device picker

See Also

Pairing with nearby devices