我们在iOS开发中,经常遇到一些静态网页,需获取网页的标题,可以用下面方法
还有,注意webview的代理方法 - (void)webViewDidFinishLoad:(UIWebView *)webView
网页前进和回退都会调用
- (void)viewDidLoad {
[super viewDidLoad];
NSString *strurl=@"http://imoa-t.naton.cn/OA/jiaohu.html";
UIWebView *web = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
web.delegate = self;
[web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:strurl]]];
[self.view addSubview:web];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
UIWebView *web = webView;
//获取所有的html
NSString *allHtml = @"document.documentElement.innerHTML";
//获取网页title
NSString *htmlTitle = @"document.title";
//获取网页的一个值
NSString *htmlNum = @"document.getElementById('title').innerText";
//获取到得网页内容
NSString *allHtmlInfo = [web stringByEvaluatingJavaScriptFromString:allHtml];
NSLog(@"%@",allHtmlInfo);
NSString *titleHtmlInfo = [web stringByEvaluatingJavaScriptFromString:htmlTitle];
NSLog(@"%@",titleHtmlInfo);
NSString *numHtmlInfo = [web stringByEvaluatingJavaScriptFromString:htmlNum];
NSLog(@"%@",numHtmlInfo);
}