开发者

Access Meta Data from UIWebView

开发者 https://www.devze.com 2023-03-27 01:07 出处:网络
How to access Meta Data of a HTML pag开发者_JAVA百科e loaded into a UIWebView in iOS?You can access the meta tag\'s content by,

How to access Meta Data of a HTML pag开发者_JAVA百科e loaded into a UIWebView in iOS?


You can access the meta tag's content by,

NSString *graphURL = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName('facebook_share')
[0].getAttribute('content')"];

where you can change the attribute name declared in 'content'


To extract the author in

<html><head>
<meta name="Author" content="Bloody Mary">
</head><body></body></html>

wrap the javascript in a function, save it as file reportMeta.js and add it to your project.

  // file reportMeta.js
  function reportMeta(){
      alert(document.getElementsByName('author')[0].getAttribute('content'));
  }

Then load the file from webViewDidFinishLoad: and execute it:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"reportMeta" ofType:@"js"];
    NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    [webView stringByEvaluatingJavaScriptFromString:jsCode];
    [webView stringByEvaluatingJavaScriptFromString:@"reportMeta()"];
}

Or, if your JS is short you can just inline it inside Objective-C.


By injecting javascript calls -

  var metas = document.getElementsByTagName('META');
  var i;
  for (i = 0; i < metas.length; i++)
  {
      ...
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消