I am trying to get WKURLSchemeHandler to serve video files for when a WebView uses a custom url scheme. I realize that didReceive(data)
can be called multiple times so I have figured out how to load my video file in chunks and send it back.
The problem is that all of this work is being done on the main thread. I cannot find an example of how to successfully get this done on a background thread. All examples of WKURLSchemeHandler that I can find including WWDC presentation video here (near the end of the video) are all so basic. None of them show how to handle large file let alone how to push work off of main thread.
And if I simply wrap everything inside a DispatchQueue.global(qos: .background).async {...}
then my app crashes b/c WebView throws an unmanaged exception with the error this task has already been stopped
!
Anyone knows how to successfully do this?
I finally figured it out. I can't believe how difficult this was. No wonder Apple hasn't released any samples around this. Here's my code: