2

Very new to XCode here

Basically, we have an app dependent on the UIWebView. When there is a valid network connection, we load a mobile web page into the UIWebView, if there is no connection, we will load a local version of the html web page. My scenario has to do with the offline or limited network connection scenario. When this offline scenario does occur, I am able to load my local html file just fine using the answer from this thread:

Load resources from relative path using local html in uiwebview

My problem comes in when click on a simple html link (which is also within my local app directory) within the local html file that is loaded. I have tried these, but when I click the link, nothing occurs, the link does not take me anywhere and the simulator will only allow me to copy the text:

<a href="file:///Sample.html">
<a href="file://Sample.html">
<a href="Sample.html">
<a href="/Sample.html">
<a href="Testing/Sample.html">

The sample.html webpage is in the same directory as the initial loaded html page. Not sure where I am going wrong, obviously missing something simple.

7
  • Actually the html in a webpage has its own directory structure at the server. So if you click a link in a webpage (online mode) it is referred to that link in the server directory. In offline mode, you have just downloaded the HTML content of the page but you don't have access to that server directory to actually hit the link. got it? Commented Jul 30, 2014 at 18:13
  • I think I understand what you are saying. But I have pulled the server content and pages onto my local directory. Couldn't I then refer to my sample.html page since it is also in my local directory (in the app itself)? Commented Jul 30, 2014 at 18:21
  • Now That is the Actual Question Dude !. Try accessing the same in Safari browser in iOS simulator and share the result Commented Jul 30, 2014 at 18:22
  • I edited it a bit to try and make it more obvious Commented Jul 30, 2014 at 18:24
  • Can you please add some code to your question that shows maybe the section surrounding the link, and how you are setting the webview url. Commented Aug 5, 2014 at 20:06

3 Answers 3

8
+50

I suggest you re-examine your directory hierarchy. The behavior you are describing is what happens when the link you are trying to navigate to does not exist on the local device. I ran the following small test:

Added a Webview to a view controller and loaded page1.html

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 10, 320, 300)];
[self.view addSubview:webView];

NSURL *url = [[NSBundle mainBundle] URLForResource:@"page1" withExtension:@"html"];
NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSURL *baseUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[webView loadHTMLString:html baseURL:baseUrl];

page1.html

<html>
<body>
    <h1>Page 1</h1>
    <a href="page2.html">Go to Page2</a>
</body>
</html>

page2.html

<html>
    <body>
        <h1>Page 2</h1>
        <a href="page1.html">Back to Page 1</a>
    </body>
</html>

Image of the project Structure

Image of the project structure

The end result is, that it works. Clicking takes you from page 1 to page 2 and back again all using local files. If we change the link to:

<a href="page3.html"></a>

which does not exist, you get the same behavior you are experiencing. That is you can only cut and copy and not actually go to the link.

enter image description here enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your example! It helped me a lot :) I was trying to have a base class that was controlling where the page would navigate in case of an internet connection (and in case of no internet connection), but I was having trouble finding a way to get it to recognize these local html pages, thanks again!
It seems that the method "loadHTMLString" does not seems to cooperate :) let urlString = Bundle.main.url(forResource: "Page1", withExtension: "html") webView.loadRequest(URLRequest(url:urlString!)) Although this seems to work.
1

For using resources from local file system try this solution like I did for displaying image on WebView.

-(NSString *)imagePath:(NSString *)fileName
{
    if ([fileName isEqualToString:@""] == NO) {
        NSLog(@"%@",fileName);
        NSString *ImagePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"png"];
        ImagePath = [ ImagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
        ImagePath = [ ImagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
        ImagePath = [@"file://" stringByAppendingString:ImagePath];
        return ImagePath;
    }

    else
    {
        return @"";
    }
}

Just assume you wnat to return the full path of your save HTML pages.

Comments

0

@Chris has an excellent example laid out, may be we need to update as follows :

    let webView  = UIWebView(frame: self.view.frame)
    webView.delegate = self
    self.view.addSubview(webView)

    let  urlString = Bundle.main.url(forResource: "Page1", withExtension: "html")

    webView.loadRequest(URLRequest(url:urlString!))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.