Loading localized html file from WKWebView

Hello all,


!! Objective-C !!


I'm in the process of updating an old app and am working on help screens.

I chose to display some useful How-tos through some html mainly for the ease of formatting the text (bullet points, colors, bold, underline etc...).

Since the app is a bit complex, The help screens are in a total of 3:

A main info webpage that links to 2 other detailed pages.


Tha app is localized in English (Base) and French.


Once the setup was complete, and with evrything working fine, I went ahead and localized the base html files.


Now, they are neatly placed in the Base.lproj and fr.lproj files.


But when when I tap on a link to another page from the main WebView, nothing happens !


Any ideas ?


Thanks


Here are some bits of code I'm using:

In the .h file :


@interface howToViewController : UIViewController <WKUIDelegate>
{
    WKWebView *infoText;
    NSString *uRLString;
}
@property (nonatomic, retain) WKWebView *infoText;


In the .m file:

- (void)viewDidLoad
{
    [super viewDidLoad];
  
    WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
    WKWebView *aWebView = [[WKWebView alloc] initWithFrame:self.view.frame
                                             configuration:theConfiguration];
    aWebView.UIDelegate = self;
    self.infoText = aWebView;
  
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"infoHTML" withExtension:@"html"];
    NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    NSURL *baseUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];

    [infoText loadHTMLString:html baseURL:baseUrl];
  
    [self.view addSubview:infoText];
}


In the html file, the links that no longer work once the files are localized:

<p>3/ More information is available for the respective tabs by pressing the following links :</p>
        <ul><li><a href="pageOne.html">Limitations</a>
        </li></ul>
        <ul><li><a href="pageTwo.html">Fueling</a>
        </li></ul>

Replies

There’s a couple of problems with the code you posted but the big one is that you’re not using

-loadFileURL:allowingReadAccessToURL:
to set up your web view. This is required if you want the web view to be able to link to other local files.

The best way to resolve this depends on your specific requirements. If your help text is all localised — meaning that

pageOne.html
and
pageTwo.html
point to localised pages — then it’d probably be best to put all the localised files in a subdirectory and point into that. So your app structure would look something like this:
MyShiny.app/
    en.lproj/
        help/
            index.html
            pageOne.html
            …
    fr.lproj/
        help/
            index.html
            pageOne.html
            …

and your code would look something like this:

NSBundle * bundle;

NSURL * indexURL = [NSBundle.mainBundle URLForResource:@"index" 
    withExtension:@"html" 
    subdirectory:@"help"
];
NSURL * baseURL = [indexURL URLByDeletingLastPathComponent];
[self.webView loadFileURL:indexURL allowingReadAccessToURL:baseURL];

Note I didn’t have time to run the above through the compiler, so please excuse any typos and so on.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks Eskimo!


Will try this and report back...

So, tried it, no joy...


Moving on to buttons on upper part of screen to load specific html files...


Thanks for the help though!

Moving on to buttons on upper part of screen to load specific html files...

I think that sentence is missing some words.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

What I meant was that I am reverting to the not ideal solution of adding a couple of buttons in a Title bar to load specific help pages...


Regards,

What I meant was that I am reverting to the not ideal solution …

Well, that’s not good. The approach I outlined on 9 Oct should work. If you put that code into a small test project, with some small test pages, does that work?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hello Eskimo,


Sory I wasn't clear.

I did try your code, to no avail...


Still not working consistently.


Pressedby time, I reverted to the stable solution of loading different html files directly from buttons on top of the screen.

Not a bad solution UI wise...


Regards,


S