iOS通过js获取webview源代码

2017-03-21  本文已影响424人  coder_hong

在webView的代理方法中- (void)webViewDidFinishLoad:(UIWebView *)webView同过JS于OC交互获取

#pragma WebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *JsToGetHTMLSource = @"document.getElementsByTagName('html')[0].innerHTML";
    NSString *HTMLSource = [webView stringByEvaluatingJavaScriptFromString:JsToGetHTMLSource];
    NSLog(@"%@",HTMLSource);
}

下面还有一些常用的

NSString *title = [webview stringByEvaluatingJavaScriptFromString:@"document.title"];
NSString *url = [webview stringByEvaluatingJavaScriptFromString:@"document.location.href"];
上一篇下一篇

猜你喜欢

热点阅读