簡體   English   中英

我正在創建一個Mac應用程序,該應用程序需要讀取和寫入HTML文件。 我該如何實現?

[英]I'm creating a Mac application that needs to read and write to/from an HTML file. How do I achieve this?

我的項目中包含一個HTML文件。 我用它來初始化一個顯示自定義Google Map的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函數的更好方法,請隨時告訴我。

從文件讀取

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"];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM