0

I am trying load local HTML file in IOS using Xcode 4.6, but i am unable to load it, it's not showing anything on simulator

my code is

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"LocalPage" ofType:@"html"     inDirectory:nil];
    NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
    [self.webView loadHTMLString:htmlString baseURL:nil];
}

Can any one please suggest me what is the problem?

Mani
  • 17,549
  • 13
  • 79
  • 100
user3153083
  • 571
  • 1
  • 5
  • 8

2 Answers2

0

Try doing some error checking:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSError *error = nil;
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"LocalPage" ofType:@"html" inDirectory:nil];
    NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:error];
    if(htmlString)
        [self.webView loadHTMLString:htmlString baseURL:nil];
    else
        NSLog( @"error in loading string from file %@ - %@", htmlFile, [error localizedDescription]);
}

It may be that the .html file doesn't exist where you think it should be, or maybe it's an empty file.

Also, take advantage of the "NSError" parameter that you can use.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
0

You can load file by URL

NSURL *htmlURL = [[NSBundle mainBundle] URLForResource:@"LocalPage" withExtension:@"html"];
[self.webView loadRequest:[NSURLRequest requestWithURL:htmlURL]];
Bilal Saifudeen
  • 1,677
  • 14
  • 14