I have a webpage which has data within a javascript object in it. I would like to get access to this data from the UIWebView to use within the native ObjectiveC code.
I have tried a number of ways
- In the delegate method
-(void) webViewDidFinishLoad:(UIWebView *) webViewI tried accessing the object using
[myWebView stringByEvaluatingJavaScriptFromString:@""];
but there didnt seem an easy way to pull in the structured data
- Other option I thought was to
just load the HTML as a string and parse it but this seemed like a really ugly approach.
An example of the HTML I have is.
<html>
<head>
<script type="text/javascript">
$(function() {
myData[0] = {
name: 'data title',
data: [
[1281880800000, 4],[1282485600000, 12],[1283090400000, 15],[1283695200000, 107],[1284300000000, 11],[1284904800000, 14],[1285509600000, 3],[1286110800000, 10],[1286715600000, 12],[1287320400000, 2],[1287925200000, 304],[1288530000000, 18],[1289134800000, 22],[1289739600000, 5],[1290344400000, 6],[1290949200000, 14],[1291554000000, 6],[1292158800000, 10],[1292763600000, 9],[1293368400000, 0],[1293973200000, 4],[1294578000000, 2],[1295182800000, 17],[1295787600000, 6],[1296392400000, 4],[1296997200000, 7],[1297602000000, 50],[1298206800000, 2],[1298811600000, 3],[1299416400000, 3],[1300021200000, 301],[1300626000000, 1],[1301230800000, 72],[1301839200000, 6],[1302444000000, 5],[1303048800000, 2],[1303653600000, 5],[1304258400000, 7],[1304863200000, 8],[1305468000000, 34],[1306072800000, 12],[1306677600000, 6],[1307282400000, 2],[1307887200000, 2],[1308492000000, 7],[1309096800000, 30],[1309701600000, 63],[1310306400000, 3],[1310911200000, 9],[1311516000000, 3],[1312120800000, 4] ],
color: '#ff1430'
};
myData[1] = {
name: 'Other Data',
data: [
[1281880800000, 29],[1282485600000, 402],[1283090400000, 0],[1283695200000, 1],[1284300000000, 1],[1284904800000, 0],[1285509600000, 0],[1286110800000, 0],[1286715600000, 1],[1287320400000, 10],[1287925200000, 2],[1288530000000, 0],[1289134800000, 10],[1289739600000, 1],[1290344400000, 3],[1290949200000, 26],[1291554000000, 2],[1292158800000, 5],[1292763600000, 3],[1293368400000, 1],[1293973200000, 3],[1294578000000, 26],[1295182800000, 2],[1295787600000, 0],[1296392400000, 5],[1296997200000, 47],[1297602000000, 36],[1298206800000, 12],[1298811600000, 21],[1299416400000, 0],[1300021200000, 0],[1300626000000, 16],[1301230800000, 0],[1301839200000, 3],[1302444000000, 4],[1303048800000, 2],[1303653600000, 0],[1304258400000, 1],[1304863200000, 5],[1305468000000, 2],[1306072800000, 1],[1306677600000, 4],[1307282400000, 1],[1307887200000, 1],[1308492000000, 51],[1309096800000, 77],[1309701600000, 3],[1310306400000, 2],[1310911200000, 1],[1311516000000, 10],[1312120800000, 4] ],
color: '#007396'
};
</script>
</head>
<body>
</body>
</html>
EDIT: To clarify I would like to be able to get access to the myData structure in the above HTML example within objective C as a NSDictionary or similar.
NSInteger x = [[[[[myDataArray objectAtIndex:0] objectForKey:@"data"] objectAtIndex:0] objectAtIndex:0]
I have used http://stig.github.com/json-framework for converting JSON into NSDictionary but was wondering how I could combine this to access this javascript object from within the HTML