Skip to content

couchbase-examples/ios-swift-quickstart-p2p

Repository files navigation

Couchbase Lite Swift Quick Start App : Multipeer Replicator

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.

Prerequistics

  • Couchbase Lite Swift v3.3.0
  • XCode 16

Build and Run

  1. Clone this Github repository

    $git clone https://github.com/couchbase-examples/ios-swift-quickstart-p2p.git
    
  2. Download CouchbaseLiteSwift v3.3.0 and copy the CouchbaseLiteSwift.xcframework into the Frameworks directory.

  3. Open the xcodeproj file in XCode, then build and run the application on a physical iOS device.

Note

  • Peer-to-peer replication must be run on physical iOS devices, as the DNS-SD feature 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 Access application, right click the System keychain of the left side panel, and click unlock.

Inside the App

Setup Required Permissions

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>

DatabaseManager

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages