I'm a bit of a newbie to Swift programming, and may be using some construct that is making a value immutable after it is set once. I have a WKWebView that is running a JavaScript webapp, and in that JS a URL is being called like this:
var url = 'testscheme://x-callback/test?unique=' + new Date().getTime() + 'Z';
window.location = url;
Ext.toast(url, 30000);
which in turn fires the extension below in my Swift code. Trouble is, the first time the extension is called, it has the correct URL that came from the JS; but after that it seems that the first URL is "stuck" somehow and anytime it is called again, it continues to print the first URL values instead of values from any subsequent calls which have different information in them.
extension VCWebView: WKURLSchemeHandler {
// any requests for a URL scheme should arrive here
func webView(_ webView: WKWebView, start urlSchemeTask: WKURLSchemeTask) {
print("In WKURLSchemeHandler webView()")
print("url absolute string requested: " + (urlSchemeTask.request.url?.absoluteString ?? "{nil}"))
// THE LINE above is where it should be printing a unique value every time it is run,
// but gets stuck with the value from the first time it was run
}
// not used
func webView(_ webView: WKWebView, stop urlSchemeTask: WKURLSchemeTask) {
}
}
I know that the JS is sending unique information, because when I close the app, and start it again, it again has a unique value the first time, but then continues to show that first unique value any time it is run again after that. There are some log file entries that I do not understand (the 4 at 09:42:01 after the second attempt):
testscheme url scheme registered
in VCWebView.swift viewDidLoad()
{1ST ATTEMPT}
09:41:12.904692-0700 MyApp[51948:6316047] [ProcessSwapping] 0x10a1842c0 - ProvisionalPageProxy::ProvisionalPageProxy: pageID = 6 navigationID = 4 suspendedPage: 0x0
In WKURLSchemeHandler webView()
url absolute string requested: testscheme://x-callback/test?unique=1570293672897Z {THIS IS WHAT JS SENT: ...897Z so this one is correct}
{2ND ATTEMPT}
09:42:01.516773-0700 MyApp[51948:6316047] [ProcessSwapping] 0x10a1842c0 - ProvisionalPageProxy::didFailProvisionalLoadForFrame: pageID = 6, frameID = 1, navigationID = 4
09:42:01.520345-0700 MyApp[51948:6316047] [ProcessSwapping] 0x10a184420 - ProvisionalPageProxy::ProvisionalPageProxy: pageID = 6 navigationID = 5 suspendedPage: 0x0
09:42:01.632990-0700 MyApp[51948:6316047] [ProcessSuspension] 0x10a1f72d0 - ProcessAssertion::processAssertionWasInvalidated()
09:42:01.633186-0700 MyApp[51948:6316047] [ProcessSuspension] 0x10a1f7f00 - ProcessAssertion::processAssertionWasInvalidated()
In WKURLSchemeHandler webView()
url absolute string requested: testscheme://x-callback/test?unique=1570293672897Z {THIS IS WHAT JS SENT: ...512Z}
09:42:31.520976-0700 MyApp[51948:6316047] [assertion] Error acquiring assertion: <NSError: 0x600002efdcb0; domain: RBSAssertionErrorDomain; code: 2; reason: "Specified target process does not exist">
09:42:31.521453-0700 MyApp[51948:6316047] Could not signal service com.apple.WebKit.WebContent: 113: Could not find specified service
Those log file entries probably explain why it is failing, but I do not know how to fix it. Any ideas what I am doing wrong?
This is the hack that I came up with that seems to work (it's so ugly - but I could not find any other way to make this work):