Created
August 24, 2017 14:58
-
-
Save Dimillian/ce1cbdfe8717c08969b78b50d9dcbacb to your computer and use it in GitHub Desktop.
WKWebviewURLHandler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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