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.
Asked
Active
Viewed 73 times
0
-
1No, you need to make that call from native code – Paulw11 Sep 10 '22 at 21:17
-
You can still do it but trigger it natively after a certain time. Like 1 min after the app has launched, or something like that (I'm using the most simple example). This answer might help you: https://stackoverflow.com/a/29155499/5024088 If you want to trigger the app rating from WebView, then that would be tricky. You need to implement JS function in the HTML file, then handle the response in Swift (once you handle the response of the JS function, you can trigger it natively as on the link above). Please specify clearly from where you want to trigger the app rating. – Emm Sep 10 '22 at 22:42
1 Answers
0
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
.

hallo
- 825
- 1
- 7
- 10