In this repository, I describe an idea on how to establish a peer connection via WebRTC between an iOS app and a macOS app both running a web app in a WKWebView.
The steps to establish the connection are:
- Offerer creates the offer SDP
- Offerer collects ICE candidates
- Offerer builds a custom scheme URI containing the offer and the candidates
- Offerer presents Share Sheet with the iOS answerer custom scheme URI
- User uses AirDrop to drop the offer onto iOS answerer
- Answerer receives the offer SDP with ICE candidates
- Answerer creates the answer SDP
- Answerer collects ICE candidates
- Answerer builds a custom scheme URI containing the answer and the candidates
- Answerer presents Share Sheet with the macOS offerer custom scheme URI
- User uses AirDrop to drop the answer onto macOS offerer
- Offerer receives the answer SDP with ICE candidates
In this example, macOS is the offerer and iOS is the answerer.
After these 10 programmatic and 2 manual steps, the WebRTC connection should get established.
This is meant for local network peer to peer connection establishment, even though technically the collected candidates can be of type that allows peer connection across networks.
The main drawback of this approach is that ICE cannot be renegotiated after connectivity loss without the devices being in close proximity physically, because of AirDrop operating over Wi-Fi.
My application is two distinct applications, one for iOS and one for macOS, each
with its own local storage (using FileManager
) syncing every now and then to
keep their data in sync.
The synchronization is something that doesn't have to be happening continuously, instead, synchronization is carried out manually whenever transferring from one device to another and wanting to continue working on the data originating from the other device.
At first, I though I'd use the new SwiftUI ShareLink
component to present the
Share Sheet on both macOS and iOS.
However, this component only seems to be present in SwiftUI for iOS, not macOS.
The Web Share API, navigator.share
, can be used to call up the Share Sheet,
but it will only allow sharing HTTP and HTTS URLs, not arbitrary custom scheme
URLs.
I could look into invoking the Share Sheet from Swift but without ShareLink
which is going to be cumbersome.
I could also continue looking for ways to allow navigator.share
to work while
not in a secure context, but this might be impossible.
I could use a custom scheme with WKWebView which seems to work around file:
not being treated as secure context:
https://stackoverflow.com/a/70009737/2715716