0

There are lots of questions asking how to get a custom font working in WKWebView with a local html snippet (eg. Custom Font Not Working in WKWebView Swift).

I, however, want to display a HTML snippet that includes relative references to remote files (eg. images) but I still also want to load a custom local font. Here is an illustrative example.

<html>
<head>
<style>
@font-face {
    font-family: 'Merriweather';
    src: url(ABSOLUTELOCALBUNDLEPATH/Merriweather-Regular.ttf) format("truetype")
}
</style
</head>
<body>
<img src="/someremotefile/file.jpg />
</body>
</html>

In other words I am trying to load the custom font from a local directory. But allow other references to be relative to a remote URL. The image would resolve to eg. https://google.com/someremotefile/file.jpg and the local font file would resolve to something like file:///THE/ABSOLUTE/BUNDLE/PATH/Merriweather-Regular.ttf.

Interestingly, if I load webview.loadHTMLString(mySnippet, baseURL: remoteBaseURL) -- remote images resolve fine but the font is not loaded/used. If I use Bundle.main.resourceURL as baseURL the font (even with the absolute path) is loaded correctly but the images obviously not.

I suspect this is some security feature of iOS as just loading the HTML snippet in macOS Safari loads the font fine regardless of where the .html is loaded from. I can't provide a baseURL here.

Any ideas how to solve my problem? Load a local font file and still resolve relative remote ressources correctly within iOS?

Nicolas
  • 575
  • 6
  • 19

0 Answers0