2010-12-30 15 views
10

Utknąłem na uzyskanie całkiem ładny JS do uruchomienia w moim UIWebView. W delegata widoku internetowej, mam:UIWebView stringByEvaluatingJavaScriptFromString

- (void)webViewDidFinishLoad:(UIWebView *)wView { 
    NSString *someHTML = [wView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('box')[0]"]; 
    NSString *allHTML = [wView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"]; 
    NSLog(@"someHTML: %@", someHTML); 
    NSLog(@"allHTML: %@", allHTML); 
} 

ale gdy metoda jest wywoływana, someHTML jest zerowa podczas allHTML zawiera całe ciało HTML w WebView.

Zabrakło mi JS w konsoli Safari JS i to działa dobrze (stwierdzi div z klasą „box”, więc wiem, że jej w HTML)

jakieś sugestie?

Więcej informacji:

FWIW, wynik w każdej z poniższych również zerowe

NSString *result = [self.webView stringByEvaluatingJavaScriptFromString: @"document"]; 
NSLog (@"1. result: %@", result); //should log the document object? 

result = [self.webView stringByEvaluatingJavaScriptFromString: @"document.body"]; 
NSLog (@"2. result: %@", result); //should log the document body 

result = [self.webView stringByEvaluatingJavaScriptFromString: @"document.body.getElementsByClassName"]; 
NSLog (@"3. result: %@", result); //should log a function 

result = [self.webView stringByEvaluatingJavaScriptFromString: @"document.body.getElementsByClassName('box')[0]"]; 
NSLog (@"4. result: %@", result); //should log the node 

Odpowiedz

20

Zmiana

NSString *someHTML = [wView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('box')[0]"]; 

do

NSString *someHTML = [wView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('box')[0].innerHTML;"]; 

(dodano .innerHTML)

czyni różnicę na świecie. . .

+0

możesz to sprawdzić również http://stackoverflow.com/questions/10793698/how-to-make-image-maps-clickable-using-uiwebview- in-microphone –