我正在创建一个需要读取和写入HTML文件的Mac应用程序。我该如何实现这一目标?

时间:2013-07-12 02:52:15

标签: javascript objective-c xcode macos

我的项目中包含HTML文件。我用它来初始化显示自定义Google地图的WebView。我是OSX应用程序的新手(我来自iOS),所以这对我来说都是新的...

我希望HTML文件的某些部分是“动态的”,尤其是在javascript函数中。

以下是我获取本地文件的URL并加载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];
}

这是HTML文件:

<!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>

如何动态更改javascript?我希望能够添加/删除坐标并绘制多条折线。在iOS中搞乱HTML / XML时,我使用了一些名为HPPLE的东西,它通过HTML / XML文件解析...

任何有关如何处理此问题的帮助都将受到赞赏......或者如果您知道更好的方式来调用javascript函数,请随时告诉我。

1 个答案:

答案 0 :(得分:0)

从文件中读取

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

写入文件

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

执行webScript:

NSString *href = [[aWebView windowScriptObject] evaluateWebScript:@"location.href"];