admin管理员组文章数量:1400599
I'm working with WKWebView, where I'm loading a webpage (React) to display. But my didFinish is getting triggered too early. So when my delegate notify about didFinish, then I do only see a white screen for a couple of seconds, before the actual data is loaded.
This is not working, causes white screen - before showing actual content
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
activityIndicator?.stopAnimating()
}
Instead I read this article, telling me it should be possible to achieve, by letting the webpage, notify the Swift code when loaded.
I did implement the following code, in my Xcode project
override func viewDidLoad() {
super.viewDidLoad()
let userContentController = WKUserContentController()
userContentController.add(self, name: "test")
}
extension DashboardViewController: WKScriptMessageHandler {
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if message.name == "test", let messageBody = message.body as? String {
print(messageBody)
}
}
}
But, now we are here. I don't know where to put this, since my web app is written in React.
<script>
function printHelloWorld() {
window.webkit.messageHandlers.test.postMessage("Did finish loading");
}
window.onload = printHelloWorld;
</script>
My React ponent
class Page extends React.Component<{ location, match }> {
public render() {
const content = (
<div className="row">
<p>Content</p>
</div>
);
return (
content
)
}
}
export default Page;
Can anyone tell me, how I can use window.webkit.messageHandlers.test.postMessage
within my React ponent?
I'm working with WKWebView, where I'm loading a webpage (React) to display. But my didFinish is getting triggered too early. So when my delegate notify about didFinish, then I do only see a white screen for a couple of seconds, before the actual data is loaded.
This is not working, causes white screen - before showing actual content
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
activityIndicator?.stopAnimating()
}
Instead I read this article, telling me it should be possible to achieve, by letting the webpage, notify the Swift code when loaded.
https://medium./capital-one-developers/javascript-manipulation-on-ios-using-webkit-2b1115e7e405
I did implement the following code, in my Xcode project
override func viewDidLoad() {
super.viewDidLoad()
let userContentController = WKUserContentController()
userContentController.add(self, name: "test")
}
extension DashboardViewController: WKScriptMessageHandler {
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if message.name == "test", let messageBody = message.body as? String {
print(messageBody)
}
}
}
But, now we are here. I don't know where to put this, since my web app is written in React.
<script>
function printHelloWorld() {
window.webkit.messageHandlers.test.postMessage("Did finish loading");
}
window.onload = printHelloWorld;
</script>
My React ponent
class Page extends React.Component<{ location, match }> {
public render() {
const content = (
<div className="row">
<p>Content</p>
</div>
);
return (
content
)
}
}
export default Page;
Can anyone tell me, how I can use window.webkit.messageHandlers.test.postMessage
within my React ponent?
1 Answer
Reset to default 4Try calling the function when the ponent mounts:
class Page extends React.Component<{ location, match }> {
ponentDidMount() {
window.webkit.messageHandlers.test.postMessage("Did finish loading");
}
render() {
...
}
}
It's sort of the onload for a React ponent.
More info about ponentdidmount
本文标签: webkitmessageHandlers for React to inject JavaScript to SwiftStack Overflow
版权声明:本文标题:webkit.messageHandlers for React to inject JavaScript to Swift - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744269940a2598131.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论