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);
}
下面还有一些常用的
- 获取页面title
NSString *title = [webview stringByEvaluatingJavaScriptFromString:@"document.title"];
- 获取当前的URL
NSString *url = [webview stringByEvaluatingJavaScriptFromString:@"document.location.href"];