Newbee
Newbee

Reputation: 265

Showing html file using UIWebView

I have created a UIWebView and used a HTML file to display some contents. But when I run it instead of showing the contents only the whole HTML file coding is coming in the WebView. Please help and tell me what is wrong.

UIWebView *ingradients= [[UIWebView alloc]init];
    [ingradients setFrame:CGRectMake(10, 170, 300, 300)];
    [ingradients loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"htmlfile" ofType:@"html"]isDirectory:NO]]];
    ingradients.delegate=self;
    [self.view addSubview:ingradients];

My htmlfile.html contains

<html>
<body>
<p><strong>Ingredients</strong></p>
</body>
</html>

Instead of showing "Ingredients" in bold its showing the whole coding of htmlfile.html

Upvotes: 1

Views: 546

Answers (3)

iPatel
iPatel

Reputation: 47049

In Your code you alway contain HTML code because your request always return file htmlfile with extantion .html

If you want to get specific value from HTML content you need to Parce HTML content by using Hpple. Also This is documentation with exmple that are use for parse HTML content.

In your case you use: (by using Hpple)

TFHpple *dataParser = [TFHpple hppleWithHTMLData:placesData];

    // name of place
NSString *XpathQueryString = @"//p/strong";
NSArray *listOfdata= [dataParser searchWithXPathQuery: XpathQueryString];

Upvotes: 2

rptwsthi
rptwsthi

Reputation: 10172

Try this code:

- (NSString *) rootPath{
    return [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) lastObject];
}

- (NSString *) pathFoResourse : (NSString *) resourseName ofType: (NSString *)type{
    NSString *path = [[MMSupport rootPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", resourseName, type]];
    if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
        path = [[NSBundle mainBundle] pathForResource:resourseName ofType:type];
    }
    NSLog(@"**path:%@**", path);
    return path;
}

- (void) loadDataToWebView : (CGRect) frame{
    NSString *htmlString =  [NSstring stringWithContentsOfFile:[MMSupport pathFoResourse:@"filename" ofType:@"html"] encoding:NSUTF8StringEncoding) error:nil];
    UIWebView *webView = [[UIWebView alloc] initWithFrame:frame];
    [webView loadHTMLString:htmlString baseURL:nil];
}

Upvotes: 0

MANIAK_dobrii
MANIAK_dobrii

Reputation: 6032

That's weird, I have similar code for this and html is rendered as rich text but not as plain text (like you have), the only difference I have is using fileURLWithPath: but not fileURLWithPath:isDirectory:. Here's my code:

NSString *localFilePath = [[NSBundle mainBundle] pathForResource:@"about" ofType:@"html"];
NSURLRequest *localRequest = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:localFilePath]];
[_aboutWebView loadRequest:localRequest];

Maybe you have some issues with file encoding, but as far as I guess, that should not be the case.

Upvotes: 2

Related Questions