1

我是 Swift 编程的新手,并且可能正在使用一些构造,该构造在设置一次后使值不可变。我有一个运行 JavaScript webapp 的 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

这些日志文件条目可能解释了它失败的原因,但我不知道如何修复它。任何想法我做错了什么?

4

1 回答 1

0

这是我想出的似乎可行的技巧(它太丑了-但我找不到任何其他方法来使这项工作):

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 ...
}
于 2019-12-30T18:32:40.480 回答