Zum Hauptinhalt springen

iOS

On iOS the iframe communicates with the host application through the webkit bridge. Configure a WKWebView with a script-message handler named bridge and decode the JSON string the iframe posts.

Embed and listen

struct WebView: UIViewRepresentable {
@Binding var path: NavigationPath

class Coordinator: NSObject, WKNavigationDelegate, WKScriptMessageHandler {
var webView: WKWebView?
var onMessageReceived: (Any) -> Void

required init(onMessageReceived: @escaping (Any) -> Void) {
self.onMessageReceived = onMessageReceived
super.init()
}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.webView = webView
}

// Receive message from WKWebView
func userContentController(
_ userContentController: WKUserContentController,
didReceive message: WKScriptMessage
) {
print(message.body)
self.onMessageReceived(message.body)
}
}

func onMessageReceived(body: Any) -> Void {
DispatchQueue.main.async {
path.append(body as! String)
}
}

func makeCoordinator() -> Coordinator {
return Coordinator(onMessageReceived: onMessageReceived)
}

func makeUIView(context: Context) -> WKWebView {
let coordinator = makeCoordinator()
let userContentController = WKUserContentController()
userContentController.add(coordinator, name: "bridge")

let configuration = WKWebViewConfiguration()
configuration.userContentController = userContentController

let _wkwebview = WKWebView(frame: .zero, configuration: configuration)
_wkwebview.navigationDelegate = coordinator

return _wkwebview
}

func updateUIView(_ webView: WKWebView, context: Context) {
let companyIdentifier = AppConfig.companyIdentifier
let request = URLRequest(url: URL(string: "https://iframe.legit.health/?company=" + companyIdentifier)!)

webView.load(request)
}
}

The Coordinator listens for events from the iframe through the bridge message handler. When the iframe posts an event, userContentController(_:didReceive:) fires with the JSON string in message.body; decode it to dispatch on event.message.

See the Callbacks section for the full catalogue.

Next Steps

  • Understand each callback type? See the Callbacks section.
  • Ready to retrieve the report? See the Output → API Endpoint page.
  • Want to display the results? See the Output → User Interface page.