1

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

3 Answers 3

2

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];
Sign up to request clarification or add additional context in comments.

2 Comments

just now i have noticed that when i open that html file in a browser also its showing the whole coding. can you tell me what will be the problem?
i actually tried loadHTMLstring method and put the html coding there as string parameter and its working fine.... :)
2

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.

1 Comment

just now i have noticed that when i open that html file in a browser also its showing the whole coding. can you tell me what will be the problem?
0

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];
}

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.