One Logo Pool Ball
Network · IOS API REFERENCE

NWListener

class · iOS 12.0+ · SDK 27.1

final class NWListener

An NWListener is an object that is able to receive incoming NWConnection objects by binding to a local endpoint. A listener will accept connections based on the protocols defined in its stack. For transport listeners (such as TCP and UDP), accepted connections will represent new local and remote address and port tuples. For listeners that include higher-level protocols that support multiplexing, accepted connections will represent multiplexed streams on a new or existing transport binding.

Members (23)

get · iOS 12.0+

parameters

TypeScript
get parameters(): NWParameters
Swift declaration
final let parameters: NWParameters

NWParameters used to create the listener

get · iOS 12.0+

debugDescription

TypeScript
get debugDescription(): string
Swift declaration
final var debugDescription: String { get }

A textual representation of this instance, suitable for debugging. Calling this property directly is discouraged. Instead, convert an instance of any type to a string by using the `String(reflecting:)` initializer. This initializer works with any type, and uses the custom `debugDescription` property for types that conform to `CustomDebugStringConvertible`: struct Point: CustomDebugStringConvertible { let x: Int, y: Int var debugDescription: String { return "(\(x), \(y))" } } let p = Point(x: 21, y: 30) let s = String(reflecting: p) print(s) // Prints "(21, 30)" The conversion of `p` to a string in the assignment to `s` uses the `Point` type's `debugDescription` property.

init · iOS 16.0+

init

TypeScript
static initApplicationServiceUsing(name: string, parameters?: NWParameters): NWListener
Swift declaration
convenience init(applicationService name: String, using parameters: NWParameters = .applicationService) throws

Create a listener that advertises an application service. - Parameter applicationService: The name of the application service to advertise. - Parameter using: The parameters to use for the listener.

get · iOS 13.0+

newConnectionLimit

TypeScript
get newConnectionLimit(): number
Swift declaration
final var newConnectionLimit: Int { get set }

Configure the listener's new connection limit. Use the value NWListener.InfiniteConnectionLimit to disable connection limits. If the value is not NWListener.InfiniteConnectionLimit, the value will be decremented by 1 every time a new connection is received. When the value reaches 0, the new connection handler will no longer be invoked until the the limit is increased. newConnectionLimit >= UInt32.max will be treated as infinite.

set · iOS 13.0+

newConnectionLimit

TypeScript
set newConnectionLimit(value: number)
Swift declaration
final var newConnectionLimit: Int { get set }

Configure the listener's new connection limit. Use the value NWListener.InfiniteConnectionLimit to disable connection limits. If the value is not NWListener.InfiniteConnectionLimit, the value will be decremented by 1 every time a new connection is received. When the value reaches 0, the new connection handler will no longer be invoked until the the limit is increased. newConnectionLimit >= UInt32.max will be treated as infinite.

get · iOS 12.0+

stateUpdateHandler

TypeScript
get stateUpdateHandler(): (((arg0: NWListener_State) => Promise<void>)) | null
Swift declaration
@preconcurrency final var stateUpdateHandler: (@Sendable (NWListener.State) -> Void)? { get set }

Set a block to be called when the listener's state changes, which may be called multiple times until the listener is cancelled.

set · iOS 12.0+

stateUpdateHandler

TypeScript
set stateUpdateHandler(value: (((arg0: NWListener_State) => void)) | null)
Swift declaration
@preconcurrency final var stateUpdateHandler: (@Sendable (NWListener.State) -> Void)? { get set }

Set a block to be called when the listener's state changes, which may be called multiple times until the listener is cancelled.

get · iOS 12.0+

newConnectionHandler

TypeScript
get newConnectionHandler(): (((arg0: NWConnection) => Promise<void>)) | null
Swift declaration
@preconcurrency final var newConnectionHandler: (@Sendable (NWConnection) -> Void)? { get set }

Block to be called for new inbound connections

set · iOS 12.0+

newConnectionHandler

TypeScript
set newConnectionHandler(value: (((arg0: NWConnection) => void)) | null)
Swift declaration
@preconcurrency final var newConnectionHandler: (@Sendable (NWConnection) -> Void)? { get set }

Block to be called for new inbound connections

get · iOS 13.0+

InfiniteConnectionLimit

TypeScript
static get InfiniteConnectionLimit(): number
Swift declaration
static let InfiniteConnectionLimit: Int

Static value to make the listener's new connection limit unbounded.

get · iOS 15.0+

newConnectionGroupHandler

TypeScript
get newConnectionGroupHandler(): (((arg0: NWConnectionGroup) => Promise<void>)) | null
Swift declaration
@preconcurrency final var newConnectionGroupHandler: (@Sendable (NWConnectionGroup) -> Void)? { get set }

Block to be called for new connection groups

set · iOS 15.0+

newConnectionGroupHandler

TypeScript
set newConnectionGroupHandler(value: (((arg0: NWConnectionGroup) => void)) | null)
Swift declaration
@preconcurrency final var newConnectionGroupHandler: (@Sendable (NWConnectionGroup) -> Void)? { get set }

Block to be called for new connection groups

get · iOS 12.0+

serviceRegistrationUpdateHandler

TypeScript
get serviceRegistrationUpdateHandler(): (((arg0: NWListener_ServiceRegistrationChange) => Promise<void>)) | null
Swift declaration
@preconcurrency final var serviceRegistrationUpdateHandler: (@Sendable (NWListener.ServiceRegistrationChange) -> Void)? { get set }

Set a block to be called when the listener has added or removed a registered service. This may be called multiple times until the listener is cancelled.

set · iOS 12.0+

serviceRegistrationUpdateHandler

TypeScript
set serviceRegistrationUpdateHandler(value: (((arg0: NWListener_ServiceRegistrationChange) => void)) | null)
Swift declaration
@preconcurrency final var serviceRegistrationUpdateHandler: (@Sendable (NWListener.ServiceRegistrationChange) -> Void)? { get set }

Set a block to be called when the listener has added or removed a registered service. This may be called multiple times until the listener is cancelled.

get · iOS 12.0+

port

TypeScript
get port(): (NWEndpoint_Port) | null
Swift declaration
final var port: NWEndpoint.Port? { get }

The current port the listener is bound to, if any. The port is only valid when the listener is in the ready state.

get · iOS 12.0+

queue

TypeScript
get queue(): (_Dispatch_DispatchQueue) | null
Swift declaration
final var queue: DispatchQueue? { get }

Get queue used for delivering block handlers, such as stateUpdateHandler, serviceRegistrationUpdateHandler, and newConnectionHandler. If the listener has not yet been started, the queue will be nil. Once the listener has been started, the queue will be non-nil.

method · iOS 12.0+

start

TypeScript
start(queue: _Dispatch_DispatchQueue): void
Swift declaration
final func start(queue: DispatchQueue)

Start the listener and provide a queue on which callback handlers will be executed. When the listener state moves to ready, the listener is registered with the system and can receive incoming connections. Start should only be called once on a listener, and multiple calls to start will be ignored.

get · iOS 12.0+

state

TypeScript
get state(): NWListener_State
Swift declaration
final var state: NWListener.State { get }

The current state of the listener

init · iOS 12.0+

init

TypeScript
static initUsingOn(using: NWParameters, on?: NWEndpoint_Port): NWListener
Swift declaration
init(using: NWParameters, on: NWEndpoint.Port = .any) throws

Creates a networking listener. The listener will be assigned a random port to listen on unless otherwise specified. - Parameter using: The parameters to use for the listener, which include the protocols to use for the listener. The parameters requiredLocalEndpoint may be used to specify the local address and port to listen on. - Parameter on: The port to listen on. Defaults to .any which will cause a random unused port to be assigned. Specifying a port that is already in use will cause the listener to fail after starting. - Returns: Returns a listener object that may be set up and started or throws an error if the parameters are not compatible with the provided port.

method · iOS 12.0+

cancel

TypeScript
cancel(): void
Swift declaration
final func cancel()

Cancel the listener. The cancel is asynchronous and the state will eventually transition to cancelled. Subsequent calls to cancel are ignored.

init · iOS 16.0+

init

TypeScript
static initServiceUsing(service: NWListener_Service, parameters: NWParameters): NWListener
Swift declaration
convenience init(service: NWListener.Service, using parameters: NWParameters) throws

Create a listener that advertises a service. - Parameter service: The service to advertise. - Parameter using: The parameters to use for the listener.

get · iOS 12.0+

service

TypeScript
get service(): (NWListener_Service) | null
Swift declaration
final var service: NWListener.Service? { get set }

Optional Bonjour service to advertise with the listener May be modified on the fly to update the TXT record or change the advertised service.

set · iOS 12.0+

service

TypeScript
set service(value: (NWListener_Service) | null)
Swift declaration
final var service: NWListener.Service? { get set }

Optional Bonjour service to advertise with the listener May be modified on the fly to update the TXT record or change the advertised service.

Skipped declarations

No declarations were skipped in this snapshot.

All Network types →