Skip to content

Instantly share code, notes, and snippets.

@Dimillian
Created August 24, 2017 14:58
Show Gist options
  • Save Dimillian/ce1cbdfe8717c08969b78b50d9dcbacb to your computer and use it in GitHub Desktop.
Save Dimillian/ce1cbdfe8717c08969b78b50d9dcbacb to your computer and use it in GitHub Desktop.
WKWebviewURLHandler
import Foundation
import WebKit
class GLAssetHandler: NSObject, WKURLSchemeHandler {
func webView(_ webView: WKWebView, start urlSchemeTask: WKURLSchemeTask) {
//Check that the url path is of interest for you, etc...
//Create a NSURLResponse with the correct mimetype.
let urlResponse = URLResponse(url: url, mimeType: "image/jpeg",
expectedContentLength: -1, textEncodingName: nil)
//IMPORTANT: Forward your response to the task.
urlSchemeTask.didReceive(urlResponse)
if let data = asset.decryptedData {
//IMPORTANT: Send your data to the task.
urlSchemeTask.didReceive(data)
}
//IMPORTANT: Tell the task that you're done.
urlSchemeTask.didFinish()
})
}
}
func webView(_ webView: WKWebView, stop urlSchemeTask: WKURLSchemeTask) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment