Search code examples
htmlobjective-cextractstringbyevaluatingjavascr

not working stringByEvaluatingJavaScriptFromString object-c


My code:

[Web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:filePath]]];
    NSString *html = [Web stringByEvaluatingJavaScriptFromString: @"document.getElementsByClassName('bottomwideborder')[1].innerHTML;"];
    NSLog(@"%@", html);

Not working for this html file - http://www.mosgortrans.org/pass3/shedule.php?type=avto&way=0&date=0000011&direction=AB&waypoint=1 (I try to extract table from html)

Please help me!


Solution

  • I'm supposing you are using Cocoa Touch (iOS), for Cocoa (Mac OS X) it's a bit different.
    loadRequest starts an asynchronous client request.
    You should assign a delegate to your web view that implements UIWebViewDelegate protocol and move your code into the webViewDidFinishLoading method:

    - (void)webViewDidFinishLoad:(UIWebView *)webView
    {
        NSString *html = [webView stringByEvaluatingJavaScriptFromString: @"document.body.getElementsByClassName('bottomwideborder')[1].innerHTML;"];
        NSLog(@"%@", html);
    }