0

I have an HTML file included in my project. I use it to initialize a WebView that displays a custom Google Map. I'm brand new to OSX apps (I'm coming from iOS) so this is all kinda new to me...

There are certain parts of the HTML file that I would like to be "dynamic", especially in the javascript function.

Here is how I get the URL for the local file and load the WebView:

-(void)awakeFromNib
{
    NSString *indexPath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
        [[mapView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:indexPath]]]; 
    [[[mapView mainFrame] frameView] setAllowsScrolling:NO];
    [mapView setNeedsDisplay:YES];
}

Here is the HTML file:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
</head>

<body style="width:100%;height:100%">
    <div id="map_area" style="position: absolute; top: 0; right: 0; bottom: 0; left: 0">       </div>
    <script type="text/javascript">
        //map = new GMap2(document.getElementById("map_area"));
        //map.setCenter(new GLatLng(37.4419, -122.1419), 13);
        var myLatLng = new google.maps.LatLng(0, -180);
        var mapOptions = {
            zoom: 1,
            center: myLatLng,
            mapTypeId: google.maps.MapTypeId.TERRAIN
        };

        var map = new google.maps.Map(document.getElementById('map_area'), mapOptions);

        var flightPlanCoordinates = [
                                     new google.maps.LatLng(37.772323, -122.214897),
                                     new google.maps.LatLng(21.291982, -157.821856),
                                     new google.maps.LatLng(-18.142599, 178.431),
                                     new google.maps.LatLng(-27.46758, 153.027892)
                                     ];
        var flightPath = new google.maps.Polyline({
                                                  path: flightPlanCoordinates,
                                                  strokeColor: '#FF0000',
                                                  strokeOpacity: 1.0,
                                                  strokeWeight: 2
                                                  });

        flightPath.setMap(map);
        </script>

</body>
</html>

How can I change the javascript on the fly? I would like to be able to add/remove coordinates and draw multiple polylines. When messing with HTML/XML in iOS, I had used something called HPPLE which parsed through HTML/XML files...

Any help in how to approach this would be appreciated... or if you are aware of a better way to call javascript functions feel free to let me know.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Johnny Gamez
  • 259
  • 6
  • 19

1 Answers1

0

read from file

NSString * selfilecontent = [NSString stringWithContentsOfFile:path];
[texteditor setString:selfilecontent]; //shown in textView

write to file

[[[texteditor textStorage]string] writeToFile:@"/Volumes/test1.rtf" atomically:YES encoding:NSUnicodeStringEncoding error:nil];

execute webScript:

NSString *href = [[aWebView windowScriptObject] evaluateWebScript:@"location.href"];
Muruganandham K
  • 5,271
  • 5
  • 34
  • 62
  • I've figured out some stuff now... But I read somewhere that I can't read/write from/to a file at run-time? Or does that pertain to iOS only? And if not, where does someone conventionally store files needed by their Cocoa app? – Johnny Gamez Jul 16 '13 at 23:36
  • look at [here](http://juliuspaintings.co.uk/cgi-bin/paint_css/animatedPaint/034-NSDocument-NSTextView.pl) and [here](http://juliuspaintings.co.uk/cgi-bin/paint_css/animatedPaint/033-NSDocument.pl) . we can store files using `NSApplicationSupportDirectory`,`NSApplicationDirectory` and `NSDocumentDirectory` at respected locations. – Muruganandham K Jul 17 '13 at 04:26
  • eg: `NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *privateDocs = [documentsDirectory stringByAppendingPathComponent:@"PSDB"];' – Muruganandham K Jul 17 '13 at 04:29
  • we can read and write files at runtime in both ios and osx [ref:[stackover flow](http://stackoverflow.com/a/11078682/2515572)]. – Muruganandham K Jul 17 '13 at 04:34