iOS
Assuming you have already integrated the Scrimmage Rewards Widget into your iOS application, you can listen to
events emitted by the widget by adding a JavaScript interface to the WKWebView.
import WebKit
struct ScrimmageRewardsView: UIViewRepresentable {
let url: URL
let token: String
func makeUIView(context: Context) -> WKWebView {
// ...
webView.load(URLRequest(url: url.appendingPathComponent("?token=\(token)")))
// Add the JavaScript interface to handle messages with ScrimmageChannel name
webView.configuration.userContentController.add(context.coordinator, name: "ScrimmageChannel")
// ...
}
// Coordinator is used to handle messages from the WebView
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
// Coordinator class
class Coordinator: NSObject, WKNavigationDelegate, WKScriptMessageHandler {
var parent: WebView
init(_ parent: WebView) {
self.parent = parent
}
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if message.name == "messageHandler", let body = message.body as? String {
parent.message = body
print("Received message from WebView: \(body)")
}
}
}
}
Check out the Widget Events API for more information.
Updated about 1 year ago