Skip to content

WKURLSchemeHandler

Ken Harris edited this page Sep 8, 2020 · 2 revisions

It's not documented yet, but all WKURLSchemeHandler methods are called on main.

You can call task methods on other threads, but it's not recommended (and there's not really any point, since you need to synchronize with main, anyway, to check the task's current status).

Important!: when they say:

After this method (i.e., webView(_:stop:)) is called, your handler must not call any methods on the task object. An exception will be thrown if it does.

they literally mean it. (This is not like NSView or Powerbox where it'll work most of the time and you might be able to sneak past if you know what you're doing.) So if your webView(_:start:) does work off the main thread, it needs to:

DispatchQueue.main.sync {
    // 1. check that "stop" has not been called!
    // 2. urlSchemeTask.didReceive(...)
}

Tip: if you don't know where else to put this, you can objc_setAssociatedObject() a "stopped" flag on the urlSchemeTask itself.

Clone this wiki locally