我对Swift编程有点新手,可能正在使用一些构造,使一个值在设置一次后就不可变。我有一个运行JavaScript URL应用程序的WKWebView,在该JS中,URL被这样调用:
var url = 'testscheme://x-callback/test?unique=' + new Date().getTime() + 'Z';
window.location = url;
Ext.toast(url, 30000);这反过来会在我的Swift代码中触发下面的扩展。问题是,在第一次调用扩展时,它具有来自JS的正确URL;但在那之后,第一个URL似乎以某种方式“卡住”了,并且每当再次调用它时,它都会继续打印第一个URL值,而不是任何后续调用中包含不同信息的值。
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) {
}
}我知道JS正在发送唯一的信息,因为当我关闭应用程序并再次启动它时,它在第一次运行时会再次具有唯一的值,但在之后的任何时候再次运行它都会继续显示第一个唯一的值。有一些我不理解的日志文件条目(第二次尝试后09:42:01的4):
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这些日志文件条目可能解释了它失败的原因,但我不知道如何修复它。你知道我做错了什么吗?
发布于 2019-12-31 02:32:40
这就是我想出的似乎有效的黑客方法(它太丑陋了--但我找不到任何其他的方法来实现它):
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
// saving the URL string that comes in here, because it is garbled when it comes into the "start urlSchemeTask" above
let defaults = UserDefaults.standard
defaults.set((navigationAction.request.url?.absoluteString ?? "{nil}"), forKey: defaultsKeys.keyTwo) // save the url string
print("1. *** url was saved")
//decisionHandler(.cancel)
decisionHandler(.allow) // always allow everything (in this use case)
}
func webView(_ webView: WKWebView, start urlSchemeTask: WKURLSchemeTask) {
var urlschemestr = ""
urlschemestr = (urlSchemeTask.request.url?.absoluteString ?? "{nil}")
print("2. *** url rqstd: " + urlschemestr.prefix(20) + "..." + urlschemestr.suffix(50))
// the first time this function runs, this URL is correct, the 2nd and subsequent times, the first URL is stuck in there, so came up with a hack:
// the decidePolicyFor (above) runs FIRST and ALWAYS seems to have the correct URL, so we save it there and use it here
// so, get the saved url from there:
let defaults = UserDefaults.standard
urlschemestr = defaults.string(forKey: defaultsKeys.keyTwo)!
print("2. *** url from saved: " + urlschemestr.prefix(20) + "..." + urlschemestr.suffix(50))
let urlschemeurl = URL(string: urlschemestr)
// now you can use variabe urlschemestr and urlschemeurl as needed ...
}https://stackoverflow.com/questions/58245374
复制相似问题