Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

troop231

macrumors 603
Original poster
Jan 20, 2010
5,826
561
I'm trying to put the results of all the div classes named "thumb" of a finished UIWebView into one string.
I appreciate your help!

EDIT: Working Code (Problem solved):

Code:
- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSURLCache sharedURLCache] removeAllCachedResponses];

    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.thewebsite.com"]]];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{   
    if (!webView.isLoading)
    {
        NSString *output = [self.webView stringByEvaluatingJavaScriptFromString:@"function f()"
                                "{"
                                "var theThumbs = document.getElementsByClassName('thumb');"
                                "var string = '';"
                                "for(var i=0; i < theThumbs.length; i++)"
                                "{"
                                "string +=(theThumbs[i].innerHTML);"
                                "}"
                                "return string;"
                                "}f()"];

        NSLog(@"String is: %@", output);
    }
}
 
Last edited:
Does it stop the loop?

Sorry, but I'd prefer if you didn't answer my question with a question. The reason I asked is that I'm trying to get you to look critically at the code you've written. What functionality do you think that return() line provides at that point in the code? How do you think it solves the problem you're attempting to use it for? Etc, etc...
 
Last edited:
Sorry, but I'd prefer if you didn't answer my question with a question. The reason I asked is that I'm trying to get you to look critically at the code you've written. What functionality do you think that return() line provides at that point in the code? How do you think it solves the problem you're attempting to use it for? Etc, etc...

Nevermind.. I see what I did.
 
It might be overkill, but adding JQuery to your project would make this trivial.

JQuery makes a lot of JS trivial so if you're not familiar with it already and you're planning on writing more JS code, becoming familiar with it may be worthwhile.
 
It might be overkill, but adding JQuery to your project would make this trivial.

JQuery makes a lot of JS trivial so if you're not familiar with it already and you're planning on writing more JS code, becoming familiar with it may be worthwhile.

Thanks for the information ArtOfWarfare! I have no plans to write any more JS code in this project, but if I need it in a future project, that's what I will use!
 
If a thread is marked resolved someone reading it should be able to figure out how it was resolved.

Um, what was the fix?
 
If a thread is marked resolved someone reading it should be able to figure out how it was resolved.

Um, what was the fix?

Look in my original post..

I'm trying to put the results of all the div classes named "thumb" of a finished UIWebView into one string.
I appreciate your help!

EDIT: Working Code (Problem solved):

Code:
- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSURLCache sharedURLCache] removeAllCachedResponses];

    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.thewebsite.com"]]];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{   
    if (!webView.isLoading)
    {
        NSString *output = [self.webView stringByEvaluatingJavaScriptFromString:@"function f()"
                                "{"
                                "var theThumbs = document.getElementsByClassName('thumb');"
                                "var string = '';"
                                "for(var i=0; i < theThumbs.length; i++)"
                                "{"
                                "string +=(theThumbs[i].innerHTML);"
                                "}"
                                "return string;"
                                "}f()"];

        NSLog(@"String is: %@", output);
    }
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.