Tuesday, August 02, 2022

HTML Head Settings

Recently, I have been busy working on my iOS updates. One feature I want to add is to provide some information views in the app.

There are many ways to present information on display in an app. I could use a rich-text component or a text view with attributed strings. However, compared to HTML, I prefer to use a web viewer to display HTML content. The most recent update and recommended strategy by Apple is WKWebView. This component fully supports HTML5 features with extensive Apple support for iOS devices, such as dynamic text size, zooming and pinch support, accessibility support, and more.

ViewController With WKWebView

With the HTML strategy, I could use HTML to format my information content with css styles, images, and js scripts. The way it works is just like a web server/client fully functional environment. My project acts like a web server. I place all my HTML files, images, css files, and js files into groups. Add a view controller class with WKWebView as the main container.

  1. class MyHTML1ViewController: UIViewController {
  2.   private var webView: WKWebView!
  3.   var htmlFile: String = "" // default value, can be changed
  4.   override func viewDidLoad() {
  5.    super.viewDidLoad()
  6.    let url = Bundle.main.url(
  7.            forResource: htmlFile,
  8.            withExtension: "html")
  9.    let request = URLRequest(url: url)
  10.    webViewBase.load(request)
  11.  }
  12. ...
  13. }

Then an html file can be loaded into the view, and the content is displayed in a way similar to a web browser.


In my project, Xcode provides support for localization. All HTML files, image files, and css files can be localised based on language support in my project. Currently, I have 3 languages to support, plus a base. A total of 4 sets of files are automatically created for me in my project. Taking an HTML file as an example, 4 HTML files are created for each language, and they are placed into a subfolder named by its language identifier. The same is true for images and css files.

Here is a snapshot of my project for groups of HTML-supported files. I group them into js, css, images, and html group names.

As you can see, each localizable item has a list of supported languages. With this visual layout, I can easily pick a language file and do my translation work.

The file structure is very different from the visual view of the project. Actually, all files are in the same root folder of projects. It is really hard to find them if I want to open Finder to find a file. That's why I named all HTML-supported files with a prefix of html_.

Further investigation, I found that within my app main bundle, all HTML supported localizable files within the project are placed in subfolders by the name of language identifiers. In other words, in my app bundle, all localised files are placed in a subfolder named as the language identifier. For example, for English, "en.lproj" subfolder contains all localised files, including my HTML-supported files. One level up from language subfolders is the root path, where all none-localized files sit. For example, js files are at the root level.

As I present all this in the above pictures, I will try to explain my understanding of how web server/client works within my app project. This picture of file layout is the web server's path structure. When an html file is loaded into WKWebViewer, all related images and css files are in the same path as the html file. Up one level is the place where js files are located.

With this understanding, I can edit my html files to refer to other source files and components easily. WKWebView is something like a browser to present HTML content and let users review and interact with it. As a client, the viewer has knowledge about where to get those sources and components from my project. I call it a server.

Keep in mind, I integrated WKWebView views into my project is based on HTML support and functionality. However, my app, built by my project, does not provide any end points for external web clients to access. My app is a native iOS app running on an iOS device.

Duplication of Settings in the head Section

I have several HMTL files to be presented by a number of view controllers with WKWebView, currently about, information and setttings. There will be expanded to more, let's say n HTML contents. Four language sets are supported in my project, and might be increased to more, m languages supported. You can imagine that n x m HTML files are in my project.

One thing I realise is that within an HTML file, in the document.head section, there are many settings. For example, common and specific css source files, common and specific js libray files, and meta configuration for various settings such as view point. Some of them are the same or repeated in all HTML files.

I realised there would be a maintenance issue. If any of these common parts are changed, as I will find some interesting feature support, I have to edit all those HTML files to do just copy and paste work. Is there any better way to do the maintenance job? Actually, I had encountered some issues where I forgot to update some languages, and those language displays did not work in the way I expected.

Seeking Help from SO

StackOverflow is a community of IT programmers' participation network. There are many talented people there to support others with all kinds of questions. After struggling to find a solution, I posted my question there. I was hoping to get advice from talented people.

Before I posted my Q, I tried various ways, including using Swift within my project. There is one way to open an HMTL url from my project. Then I could do parameter replacement of the content of the HTML file as a string. However, I remembered that I tried this method before. I just could not change any files within my application bundle!

The short story is that I tried to use a plist file within my project to save some setting changes. No errors are thrown out when some new changes are saved to the plist file. However, when the settings were read out later, they were back to their original values. I had been struggling for days to find out why, and finally I found that the plist file in the project bondle cannot be changed!

I resolved the issue by copying the plist file to the application sandbox supported folder. The plist file can be edited and saved there. After that, I come to my explanation: it may be a security reason that files within the app bundle cannot be changed.

I could copy the HTML file to outside of the bondle path and make changes there. What about other HTML sources and library files? It just defeats the language support naturally provided by the iOS framework if I have to find out or identify which language is supported, and then copy all those files from specific language subfolders out.

I knew there is another way to load an HTML string directly into WKWebView. The string can be changed with a parameter replacement. This would display HTML content. However, the same issue comes back to me again. How about linking other sources and library files if I load the view with a string? No path information from the string

Since I am using HTML technology, naturally I should find a way in HTML or js to inject common head settings into my loading HTML file.

Partial Working Solution

Within about a day of my post, I got two answers. One is using js to inject common settings from a js function. I tried it immediately. It did not work at first.

When I went back to my Q, my Q was marked as [losed] by a senior member of SO. The reason is that my working environment was not explained clearly. More explanation is required.

With [losed] status, no more answers are allowed. This actually turns many people away. The only way I could ask for help and get attention was to update my Q and add comments. As you can see, I have done several updates, and the Q is quite lengthy. Anyway, I think I have done enough to clarify my Q on SO.

If you read my Q, you will see a solution in it. Based on the only post's answer with codes, I made some changes, and took the meta setting for charset out of my common settings. It works now in my project, just as I expected, to inject the common settings into the head section.

Even though my solution is working, I still have questions about it, and I would like to see if there is any other better solution or advice on this issue.

Since SO is a member participation and support community, I need to get some people's help to turn off the [close] status. What is required is to have 2 more requests at the link of reopen, at the end of my Q.