2019-11-07 21:29:16 +01:00
|
|
|
//
|
|
|
|
// AccountViewControllerSchemeHandler.swift
|
|
|
|
// NetNewsWire-iOS
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 11/7/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import WebKit
|
|
|
|
import Articles
|
|
|
|
|
|
|
|
class ArticleIconSchemeHandler: NSObject, WKURLSchemeHandler {
|
|
|
|
|
|
|
|
var currentArticle: Article?
|
|
|
|
|
|
|
|
func webView(_ webView: WKWebView, start urlSchemeTask: WKURLSchemeTask) {
|
|
|
|
|
2019-11-07 22:00:43 +01:00
|
|
|
guard let responseURL = urlSchemeTask.request.url, let iconImage = self.currentArticle?.iconImage() else {
|
|
|
|
urlSchemeTask.didFailWithError(URLError(.fileDoesNotExist))
|
|
|
|
return
|
2019-11-07 21:29:16 +01:00
|
|
|
}
|
2019-11-07 22:00:43 +01:00
|
|
|
|
|
|
|
let iconView = IconView(frame: CGRect(x: 0, y: 0, width: 48, height: 48))
|
|
|
|
iconView.iconImage = iconImage
|
|
|
|
let renderedImage = iconView.asImage()
|
|
|
|
|
|
|
|
guard let data = renderedImage.dataRepresentation() else {
|
|
|
|
urlSchemeTask.didFailWithError(URLError(.fileDoesNotExist))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let headerFields = ["Cache-Control": "no-cache"]
|
|
|
|
if let response = HTTPURLResponse(url: responseURL, statusCode: 200, httpVersion: nil, headerFields: headerFields) {
|
|
|
|
urlSchemeTask.didReceive(response)
|
|
|
|
urlSchemeTask.didReceive(data)
|
|
|
|
urlSchemeTask.didFinish()
|
|
|
|
}
|
|
|
|
|
2019-11-07 21:29:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func webView(_ webView: WKWebView, stop urlSchemeTask: WKURLSchemeTask) {
|
|
|
|
urlSchemeTask.didFailWithError(URLError(.unknown))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|