Getting background color from UIWebView html?

ios, objective-c, uiwebview

Solution

working code in only three steps ..

1)Copy code and paste it in your project. 2)Connect ___webView outlet to UIWebView in xib file. 3)Run.

- (void)viewDidLoad
{
    [___webView loadHTMLString:[NSString stringWithFormat:@"<html><body id=\"mybody\" style=\"background-color:green;width:100px;height:100px;\"><b>Hello buddy</b></body></html>"] baseURL:nil];
    ___webView.delegate=self;

    [super viewDidLoad];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *javaScript = @"document.body.style.background";
    NSLog(@"color is %@",[webView stringByEvaluatingJavaScriptFromString:javaScript]);
    NSLog(@"did load finish ");
}

//output is -----

2013-09-03 11:56:33.187 ads[3655:11303] color is green 2013-09-03 11:56:33.188 ads[3655:11303] did load finish

Problem

I've got a smaller UIWebView within a UIView. I want to set the UIView's background color to be the same as the webView's loaded HTML. Is this possible? I've tried, in `webViewDidFinishLoad`, to get the color like this: ``` NSLog(@"color: %@", [webView stringByEvaluatingJavaScriptFromString: @"document.body.style.background"]); ``` .. but it doesn't return anything.

Original source