我們在iOS開發(fā)中丹泉,經(jīng)常遇到一些靜態(tài)網(wǎng)頁情萤,需獲取網(wǎng)頁的標題,可以用下面方法
還有摹恨,注意webview的代理方法 - (void)webViewDidFinishLoad:(UIWebView *)webView
網(wǎng)頁前進和回退都會調(diào)用
- (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";
//獲取網(wǎng)頁title
NSString *htmlTitle = @"document.title";
//獲取網(wǎng)頁的一個值
NSString *htmlNum = @"document.getElementById('title').innerText";
//獲取到得網(wǎng)頁內(nèi)容
NSString *allHtmlInfo = [web stringByEvaluatingJavaScriptFromString:allHtml];
NSLog(@"%@",allHtmlInfo);
NSString *titleHtmlInfo = [web stringByEvaluatingJavaScriptFromString:htmlTitle];
NSLog(@"%@",titleHtmlInfo);
NSString *numHtmlInfo = [web stringByEvaluatingJavaScriptFromString:htmlNum];
NSLog(@"%@",numHtmlInfo);
}