The LoyaltyLion SDK supports bi-directional communication with your mobile app to facilitate more advanced features.

For example, with Shopify mobile apps this can be used to have LoyaltyLion ask your app to add a voucher code or free product to the current cart.

Setting up a WebView for advanced features

When LoyaltyLion is embedded in a WKWebView, we’ll use the WKScriptMessageHandler interface to send messages to your app.

You’ll need to register the listeners you want to support with your content controller, and then handle them appropriately. The example below listens to the ready event to detect when the Loyalty Page has finished loading.

class ViewController: UIViewController, WKScriptMessageHandler {
  override func viewDidLoad() {
    let contentController = WKUserContentController()

    // subscribe to LoyaltyLion events
    contentController.add(self, name: "sdkReady")

    // continue creating WKWebView instance...
  }

  func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
    if message.name == "ready" {
      print("LoyaltyLion SDK is ready")
    }
  }
}