Sachin Hegde Sachin Hegde - 4 months ago 51x
Javascript Question

Parsing Open Graph meta properties/tags in ObjectiveC

I have an iOS app in objectiveC which uses UIWebView to load a webpage. I want to parse the Open Graph meta tags of that web page -

<meta property="og:locale" content="en_US" />
<meta property="og:title" content="My Title" />
<meta property="og:site_name" content="My Content" />

With the following code I can iterate through all of the meta tags and get their content, but how do I check for the meta properties I am interested in?

NSString *resultStr = [self.viewWeb stringByEvaluatingJavaScriptFromString:
"var i, metaFields = document.getElementsByTagName(\"meta\"), result = [];"
"for (i = 0; i< metaFields.length; i++) {"
"result.push({\"content\": metaFields[i].content});"
"return JSON.stringify(result);"
NSArray *result = [NSJSONSerialization JSONObjectWithData:[resultStr dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
NSLog(@"result: %@", result);

For example if I am only interested in the "og:site_name" how do I check for it? I tried "metaFields[i].property" but it seems that properties are not accessible that ways. I want to do something like -

"if (metaFields[i].property === \"og:site_name\")"



Have you tried using the getAttribute DOM method on the element? Something like: