Use correct base URL for web view content
This commit is contained in:
parent
755f98a2e2
commit
85819ea6aa
|
@ -53,9 +53,7 @@ class ReadViewController: UIViewController {
|
||||||
webView.navigationDelegate = self
|
webView.navigationDelegate = self
|
||||||
webView.uiDelegate = self
|
webView.uiDelegate = self
|
||||||
if let content = itemContentHTML() {
|
if let content = itemContentHTML() {
|
||||||
// todo: using the bundle url is the only way to get the stylesheet to load, but feels wrong
|
webView.loadHTMLString(content, baseURL: item.url)
|
||||||
// will break, e.g., images with relative urls
|
|
||||||
webView.loadHTMLString(content, baseURL: Bundle.main.bundleURL)
|
|
||||||
}
|
}
|
||||||
view.addSubview(webView)
|
view.addSubview(webView)
|
||||||
|
|
||||||
|
@ -68,6 +66,8 @@ class ReadViewController: UIViewController {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static let css = try! String(contentsOf: Bundle.main.url(forResource: "read", withExtension: "css")!)
|
||||||
|
|
||||||
private func itemContentHTML() -> String? {
|
private func itemContentHTML() -> String? {
|
||||||
guard let content = item.content else {
|
guard let content = item.content else {
|
||||||
return nil
|
return nil
|
||||||
|
@ -100,7 +100,7 @@ class ReadViewController: UIViewController {
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no" />
|
||||||
<link rel="stylesheet" href="\(Bundle.main.url(forResource: "read", withExtension: "css")!.absoluteString)" />
|
<style>\(ReadViewController.css)</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="item-info">
|
<div id="item-info">
|
||||||
|
@ -119,7 +119,7 @@ class ReadViewController: UIViewController {
|
||||||
extension ReadViewController: WKNavigationDelegate {
|
extension ReadViewController: WKNavigationDelegate {
|
||||||
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction) async -> WKNavigationActionPolicy {
|
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction) async -> WKNavigationActionPolicy {
|
||||||
let url = navigationAction.request.url!
|
let url = navigationAction.request.url!
|
||||||
if url == Bundle.main.bundleURL {
|
if url == item.url {
|
||||||
return .allow
|
return .allow
|
||||||
} else {
|
} else {
|
||||||
present(SFSafariViewController(url: url), animated: true)
|
present(SFSafariViewController(url: url), animated: true)
|
||||||
|
|
Loading…
Reference in New Issue