NSHYJ

个人站

write the code, change the world.


iOS webview

capture html property meta in webview

测试url:www.fang.com
在webview的webViewDidFinishLoad中:


- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *str1 = [webView stringByEvaluatingJavaScriptFromString:@"document.querySelector(\"meta[name=description]\").getAttribute('content')"];
    NSString *str2 = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    NSLog(@"===str1:%@,str2:%@====",str1,str2);

}


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

    NSURL *url = [request URL];
    
    //capture click link in webview
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        self.urlStr = url.absoluteString;
    }
    return YES;

}