Stay organized with collections
Save and categorize content based on your preferences.
Initiate a connection
When nearby devices are found, the discoverer can initiate connections. The
following example requests a connection with a device as soon as it is
discovered.
Swift
extensionExample:DiscovererDelegate{funcdiscoverer(_discoverer:Discoverer,didFindendpointID:EndpointID,withcontext:Data){// An endpoint was found. We request a connection to it. The endpoint info can be used// to provide arbitrary information to the discovering device (e.g. device name or type).discoverer.requestConnection(to:endpointID,using:"My Device".data(using:.utf8)!)}funcdiscoverer(_discoverer:Discoverer,didLoseendpointID:EndpointID){// A previously discovered endpoint has gone away.}}
Depending on your use case, you may wish to instead display a list of discovered
devices to the user, allowing them to choose which devices to connect to.
Accept or reject a connection
After the discoverer has requested a connection to an advertiser, the advertiser
is notified of the connection request via the
advertiser(_:didReceiveConnectionRequestFrom:with:connectionRequestHandler:)
delegate method.
Swift
extensionExample:AdvertiserDelegate{funcadvertiser(_advertiser:Advertiser,didReceiveConnectionRequestFromendpointID:EndpointID,withcontext:Data,connectionRequestHandler:@escaping(Bool)->Void){// Call with `true` to accept or `false` to reject the incoming connection request.connectionRequestHandler(true)}}
Once the advertiser accepts, both side are notified and must verify the
connection via the connectionManager(_:didReceive:from:verificationHandler:)
delegate method.
It is recommended that your app verifies the connection by using the
verification code provided by the delegate method. This provides a way to let
users confirm that they are connecting to the intended device. Both devices are
given the same code, which is a short random string; it's up to you to decide
how to verify it. Typically this involves showing the token on both devices and
asking the users to manually compare and confirm, similar to a bluetooth pairing
dialog.
Swift
extensionExample:ConnectionManagerDelegate{funcconnectionManager(_connectionManager:ConnectionManager,didReceiveverificationCode:String,fromendpointID:EndpointID,verificationHandler:@escaping(Bool)->Void){// Optionally show the user the verification code. Your app should call this handler// with a value of `true` if the nearby endpoint should be trusted, or `false`// otherwise.verificationHandler(true)}}
The connection is fully established only when both sides have accepted. If one
or both reject, the connection is discarded.
The above examples shows the connection being automatically accepted by both
sides, but depending on your use case you may wish to present this choice to the
user in some way.
[null,null,["Last updated 2025-08-12 UTC."],[[["\u003cp\u003eNearby Connections API enables devices to discover each other and initiate connections for data exchange.\u003c/p\u003e\n"],["\u003cp\u003eUpon discovery, the discovering device can request a connection, which the advertising device can accept or reject.\u003c/p\u003e\n"],["\u003cp\u003eSecure connections require mutual verification using a code, typically displayed to users for confirmation.\u003c/p\u003e\n"],["\u003cp\u003eConnection establishment involves both sides accepting the request and verifying, if enabled, before data transfer can begin.\u003c/p\u003e\n"],["\u003cp\u003e\u003cstrong\u003eCaution:\u003c/strong\u003e Connections without verification are insecure and vulnerable to exploitation, prioritize secure practices for robust protection.\u003c/p\u003e\n"]]],[],null,["# Manage connections\n\nInitiate a connection\n---------------------\n\nWhen nearby devices are found, the discoverer can initiate connections. The\nfollowing example requests a connection with a device as soon as it is\ndiscovered. \n\n### Swift\n\n extension Example: DiscovererDelegate {\n func discoverer(\n _ discoverer: Discoverer, didFind endpointID: EndpointID, with context: Data) {\n // An endpoint was found. We request a connection to it. The endpoint info can be used\n // to provide arbitrary information to the discovering device (e.g. device name or type).\n discoverer.requestConnection(to: endpointID, using: \"My Device\".data(using: .utf8)!)\n }\n\n func discoverer(_ discoverer: Discoverer, didLose endpointID: EndpointID) {\n // A previously discovered endpoint has gone away.\n }\n }\n\nDepending on your use case, you may wish to instead display a list of discovered\ndevices to the user, allowing them to choose which devices to connect to.\n\nAccept or reject a connection\n-----------------------------\n\nAfter the discoverer has requested a connection to an advertiser, the advertiser\nis notified of the connection request via the\n`advertiser(_:didReceiveConnectionRequestFrom:with:connectionRequestHandler:)`\ndelegate method. \n\n### Swift\n\n extension Example: AdvertiserDelegate {\n func advertiser(\n _ advertiser: Advertiser, didReceiveConnectionRequestFrom endpointID: EndpointID,\n with context: Data, connectionRequestHandler: @escaping (Bool) -\u003e Void) {\n // Call with `true` to accept or `false` to reject the incoming connection request.\n connectionRequestHandler(true)\n }\n }\n\nOnce the advertiser accepts, both side are notified and must verify the\nconnection via the `connectionManager(_:didReceive:from:verificationHandler:)`\ndelegate method.\n\nIt is recommended that your app verifies the connection by using the\nverification code provided by the delegate method. This provides a way to let\nusers confirm that they are connecting to the intended device. Both devices are\ngiven the same code, which is a short random string; it's up to you to decide\nhow to verify it. Typically this involves showing the token on both devices and\nasking the users to manually compare and confirm, similar to a bluetooth pairing\ndialog. \n\n### Swift\n\n extension Example: ConnectionManagerDelegate {\n func connectionManager(\n _ connectionManager: ConnectionManager, didReceive verificationCode: String,\n from endpointID: EndpointID, verificationHandler: @escaping (Bool) -\u003e Void) {\n // Optionally show the user the verification code. Your app should call this handler\n // with a value of `true` if the nearby endpoint should be trusted, or `false`\n // otherwise.\n verificationHandler(true)\n }\n }\n\nThe connection is fully established only when both sides have accepted. If one\nor both reject, the connection is discarded.\n\nThe above examples shows the connection being automatically accepted by both\nsides, but depending on your use case you may wish to present this choice to the\nuser in some way.\n| **Caution:** While verification is optional, connections established without verification are insecure, and can expose devices to severe security vulnerabilities. To avoid this, always use verification to secure your connections."]]