Vinoth
Vinoth

Reputation:

Strip HTML from web page on iPhone

This is my code:

NSURL *url=[NSURL URLWithString:@"http://www.engadget.com"];
NSString *webPage=[[NSString alloc]initWithContentsOfURL:url
                          encoding:NSUTF8StringEncoding error:nil];

In the webPage string I got an html page of the link. In that string there is lot of tags and text. I want to take only body of the text without any tags.

I want to display that text into my UITextView. How can I do that?

Upvotes: 1

Views: 4075

Answers (3)

Biranchi
Biranchi

Reputation: 16317

This is the best answer and is exactly what you are looking for:

Write the following script in the webView delegate method. (UIWebviewdidfinishLoading)

NSString *myText = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.textContent"];

Upvotes: 1

Jimit
Jimit

Reputation: 652

Better Solution:

- (NSString *)flattenHTML:(NSString *)html {

    NSScanner *theScanner;
    NSString *text = nil;

    theScanner = [NSScanner scannerWithString:html];

    while ([theScanner isAtEnd] == NO) {

        // find start of tag
        [theScanner scanUpToString:@"<" intoString:NULL] ; 

        // find end of tag
        [theScanner scanUpToString:@">" intoString:&text] ;

        // replace the found tag with a space
        //(you can filter multi-spaces out later if you wish)
        html = [html stringByReplacingOccurrencesOfString:
                           [ NSString stringWithFormat:@"%@>", text]
                     withString:@" "];

    } // while //

    return html;

}

Reference: http://rudis.net/content/2009/01/21/flatten-html-content-ie-strip-tags-cocoaobjective-c

Upvotes: 0

Dimitris
Dimitris

Reputation: 13660

From what I tried, this did the job best. Even though the NSSCanner is not the smaerter solution for this, if the html/xml is well formed you should be fine.

Upvotes: 0

Related Questions