0

I load a html page in web view in this way:

NSString *description = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.mypage.html"] encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:pRova baseURL:nil];

I need to remove:

<img src="http://www.mypage/image" align="left" style="padding: 0px 10px 0px 0px; width: 301px; height: 280px;" alt="diaspora-uys" />

from NSString *description, to display the UIwebView without image and with only text. How can i do this?

Claudio
  • 173
  • 1
  • 2
  • 12

4 Answers4

3

I solved in this way:

- (NSString *)flattenHTML:(NSString *)html {
    NSScanner *theScanner;
    NSString *gt =nil;

    theScanner = [NSScanner scannerWithString:html];

    while ([theScanner isAtEnd] == NO) {
        // find start of tag
        [theScanner scanUpToString:@"<img" intoString:NULL] ; 

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

    html = [html stringByReplacingOccurrencesOfString:[ NSString stringWithFormat:@"%@>", gt] withString:@""];
    return html;
}  
Willy
  • 9,681
  • 5
  • 26
  • 25
Claudio
  • 173
  • 1
  • 2
  • 12
0

I have modified the code and it's working perfectly. It will only remove src value. Remaining values and image tag are not modified

- (NSString *)flattenHTML:(NSString *)html {
    NSScanner *theScanner;
    NSString *gt = nil;
    NSString *temp = nil;
    theScanner = [NSScanner scannerWithString:html];

    while ([theScanner isAtEnd] == NO) {
        // find start of tag
        [theScanner scanUpToString:@"<img" intoString:&temp]; 

        //find the src tag
        [theScanner scanUpToString:@"src" intoString:&temp]; 

        [theScanner scanUpToString:@"=" intoString:&temp];

        [theScanner scanUpToString:@" " intoString:&gt];
        if (!gt) {
            [theScanner scanUpToString:@">" intoString:&gt];    
        }

        if (gt) {
            html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@",gt] withString:@"=\"\""];    
        }
    }

    return html;
}  
Rahul Vyas
  • 28,260
  • 49
  • 182
  • 256
0

1/ load the HTML content in a string

+(id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)

2/ remove the <img /> tag in the string

3/ use loadHTMLString on you webView

Have a look at NSString reference

Instead of finding the <img /> tag in the HTML string, you can also perform a replace of <img by <img style='display:none;' to hide the image. But it will be still loaded...

Francois
  • 10,730
  • 7
  • 47
  • 80
  • You mean something like that: NSString *descrizione = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"html page"] encoding:NSUTF8StringEncoding error:nil]; NSString *example = [example stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@""] withString:@""]; [self.webView loadHTMLString:descrizione baseURL:nil]; – Claudio Nov 17 '10 at 13:27
  • It looks good to me except the replace of ``. With your code, you will end with a broken HTML code. By `` I was meaning the full `diaspora-uys`. That's this complete tag that you need to skip. – Francois Nov 17 '10 at 17:49
0

check that to apply filter on url scheme : Need to filter certain UIWebView requests and load them in different UIWebViews

Community
  • 1
  • 1
Christophe Debove
  • 6,088
  • 20
  • 73
  • 124