This quick-start app demonstrates how to use Couchbase Lite Swift to load a prebuilt database, create and update documents, and query documents. It also shows how to set up peer-to-peer replication using the Multipeer Replicator, which leverages DNS-SD (Bonjour) for automatic peer discovery over Wi-Fi, enabling synchronization between nearby devices without requiring a centralized servers.
- Couchbase Lite Swift v3.3.0
- XCode 16
-
Clone this Github repository
$git clone https://github.com/couchbase-examples/ios-swift-quickstart-p2p.git -
Download CouchbaseLiteSwift v3.3.0 and copy the
CouchbaseLiteSwift.xcframeworkinto theFrameworksdirectory. -
Open the
xcodeprojfile in XCode, then build and run the application on a physical iOS device.
-
Peer-to-peer replication must be run on physical iOS devices, as the
DNS-SDfeature used for peer discovery is not supported in the iOS Simulator. -
The application can also be run on macOS, but it requires to unlock the System keychain to store and retrieve the generated self-signed identity.
To unlock the system keychain, open the
Keychain Accessapplication, right click theSystemkeychain of the left side panel, and click unlock.
To use DNS-SD for peer discovery, the app must declare the Bonjour service type and request access to the local network. This requires adding two keys to the app’s Info.plist:
NSBonjourServices
Add the service type _couchbaseP2P._tcp, which is used by MultipeerReplicator for advertising and discovering peers as follows:
<key>NSBonjourServices</key>
<array>
<string>_couchbaseP2P._tcp</string>
</array>
NSLocalNetworkUsageDescription
Add a usage description for local network access. This can be customized.
<key>NSLocalNetworkUsageDescription</key>
<string>Used for discovering and connecting to peers.</string>
The DatabaseManager.swift contains all of the code responsible for
- Setting up a database by loading the pre-built travel sample database bundled with the app.
- Setup a full-text index and running queries.
- Retrieving or creating a self-signed identity stored in the Keychain, used for Multipeer Replicator configuration.
- Setting up and starting a Multipeer Replicator for peer-to-peer sync.