I have an app running WKWebView on a local html file, and I would like to add the function request app rating, but I don't know if it is possible and how to call that. The project runs in a XCode 13 on swift.
I have an app running WKWebView on a local html file, and I would like to add the function request app rating, but I don't know if it is possible and how to call that. The project runs in a XCode 13 on swift.
Yes, you need to register a WKUserContentController on your WKWebView like this
let webView = WKWebView()
let storeKitHandler = StoreKitHandler()
webView.configuration.userContentController.add(storeKitHandler, name: "storeKitHandler")
Where the StoreKitHandler is a class conforming to WKScriptMessageHandler
class MyHandler: NSObject, WKScriptMessageHandler {
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if message.name == "callback" {
if let body = message.body as? String, body == "requestReview" {
SKStoreReviewController.requestReview()
}
}
}
}
And then call the method like this from JavaScript
window.webkit.messageHandlers.storeKitHandler.postMessage('requestReview');
This will then call userContentController(_:WKUserContentController, didReceive: WKScriptMessage) with the message name storeKitHandler and body requestReview.