Send message between javascript and App

Hi, have developed an App which loads files from the Bundle directory to the Documents directory when the App boots up. The index file (index.html) on the App copies these files to the Documents directory and serves them from there. The index page needs to talk to the Native app code to monitor the progress. I am using this code to get an update on the file copy progress:


This sends the progress to the javascript on the index.html page

webView.evaluateJavaScript("updateProgress('\(Status)')")

This code on the index.html page talks to the native code on the App

WKScriptMessage - window.webkit.messageHandlers.updateProgress.postMessage(message)

My problem is;

When the app starts loading the pages the code above does not get fired until the files are all copied

Is there a better way for the App to talk to the index.html page as the code is running?

Replies

When the app starts loading the pages the code above does not get fired until the files are all copied

Who is doing this copying? If the copying is being done on the main thread then the thread won’t be able available to service script messages.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi eskimo thanks for having a look, The way I need it to work is like this:


1. index page loads fully and gets displayed in webview (initial splash screen if you like)

2. A timer function starts checking all files needed for the App get copied from the bundle directory into the Documents directory then the App is ready for use


The problem I have is:

The index file is called to load into webview but my timer function starts to run and my "messageHandlers" code seems to freeze until all the files are copied even though I can see the webview has loaded as shown below:


2018-04-17 16:46:26.191776+0100 GlowmMother[59494:2951761] [MC] Loaded MobileCoreServices.framework

2018-04-17 16:46:26.222492+0100 GlowmMother[59494:2951761] WKWebView(<WKWebView: 0x7f92ac843000; frame = (0 0; 414 736); layer = <CALayer: 0x6000000398c0>>) is ready for loading plugins

Apr 17 16:46:26 org.xwebview.xwebview <I>: WKWebView(<WKWebView: 0x7f92ac843000; frame = (0 0; 414 736); layer = <CALayer: 0x6000000398c0>>) is ready for loading plugins


********* webview fully loaded ********

On what thread is the timer running? Does it run multiple times? Or just once? And how long does each run take?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi eskimo


I have a function which starts a timer to copy the files after the webview loads:


func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {

print("********* webview fully loaded ********")

updaterService()

}


The updaterService is a timer function that runs every minute.... later in the Apps life, updated files can be downloaded to replace some files. That's the reason why I use the Documents directory. But as iI explained as soon as the updaterService gets called, the webview waits