This article provides instructions on explicitly configuring your Ditto instance to listen for connections on a specific port and to connect to remote instances using a host (IP) and port.
Although Ditto automatically attempts to connect to other instances on the Local Area Network (LAN), Bluetooth Low Energy (LE), and Apple Wireless Direct Link (AWDL), supplying a DittoTransportConfig does not enable this feature by default.
Rather, you manually enable peer-to-peer connections using EnableAllPeerToPeer(). (See Enabling and Disabling Transports for more information.)
// Create a new DittoTransportConfig()var config = DittoTransportConfig()//Enable all peer to peer transportsconfig.enableAllPeerToPeer()//Or enable/disable each transport separately//BluetoothLeconfig.peerToPeer.bluetoothLE.isEnabled = true//Local Area Networkconfig.peerToPeer.lan.isEnabled = true//Awdlconfig.peerToPeer.awdl.isEnabled = trueditto.transportConfig = configdo { try ditto.startSync()} catch (let err) { print(err.localizedDescription)}
If you need additional connection configurations for the current Ditto instance, configure it to listen for connections on a specific port and to connect to remote instances using a host (IP) and port:
1
Before calling startSync(), create a DittoTransportConfig object with the desired settings.
2
Using the ditto.SetTransportConfig API method, set the created object on your Ditto instance.
3
Call the startSync method on your Ditto instance to initiate sync with the configured settings.
let ditto = Ditto(identity: .onlinePlayground( appID: "REPLACE_ME_WITH_YOUR_APP_ID", token: "REPLACE_ME_WITH_YOUR_PLAYGROUND_TOKEN", // Set to false to disable syncing with the cloud enableDittoCloudSync: true))do { try ditto.startSync()} catch (let err) { print(err.localizedDescription)}
// Setting up inside a ViewControllerlet ditto = Ditto(identity: DittoIdentity.onlinePlayground(appID: "REPLACE_ME_WITH_YOUR_APP_ID", token: "REPLACE_ME_WITH_YOUR_PLAYGROUND_TOKEN"))ditto.delegate = selftry! ditto.startSync()// Now you can observe real time changes to the transport conditions:extension ViewController: DittoDelegate { func transportConditionDidChange(transportID: Int64, condition: TransportCondition) { if condition == .BleDisabled { print("BLE disabled") } else if condition == .NoBleCentralPermission { print("Permission missing for BLE") } else if condition == .NoBlePeripheralPermission { print("Permission missing for BLE") } }