简体   繁体   English

如何在UIWebView中显示本地html文件?

[英]How do I display a local html file in a UIWebView?

I have a relatively simple question that I cannot seem to find the answer for. 我有一个相对简单的问题,我似乎找不到答案。 While doing the Google Maps Java API Tutorials, I ran into a problem. 在进行Google Maps Java API教程时,我遇到了一个问题。 I can load an HTML file from the web, but when I try it locally, it just displays the contents of the file instead of running the script. 我可以从Web加载HTML文件,但是当我在本地试用它时,它只显示文件的内容而不是运行脚本。

Here's what works: 这是有效的:

NSString *url = @"http://code.google.com/apis/maps/documentation/v3/examples/geocoding-simple.html";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[webView loadRequest:request];

I want to store the HTML file locally and run it from the device itself, so I tried: 我想在本地存储HTML文件并从设备本身运行它,所以我试过:

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"geocoding-simple" ofType:@"html"]isDirectory:NO]]]; 

and it just displayed the contents of the file. 它只显示文件的内容。

Here is the html file: 这是html文件:

<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps JavaScript API v3 Example: Geocoding Simple</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
  var geocoder;
  var map;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {
      zoom: 8,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  }

  function codeAddress() {
    var address = document.getElementById("address").value;
    if (geocoder) {
      geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          map.setCenter(results[0].geometry.location);
          var marker = new google.maps.Marker({
              map: map, 
              position: results[0].geometry.location
          });
        } else {
          alert("Geocode was not successful for the following reason: " + status);
        }
      });
    }
  }
</script>
</head>
<body style="margin:0px; padding:0px;" onload="initialize()">
  <div>
    <input id="address" type="textbox" value="Sydney, NSW">
    <input type="button" value="Geocode" onclick="codeAddress()">

  </div>
<div id="map_canvas" style="width:100%; height:90%"></div>
</body>
</html>

What am I doing wrong here? 我在这做错了什么?

Thomas 托马斯

OK. 好。 I cleaned all targets, and it works now...SWEEEET! 我清理了所有目标,它现在有效...... SWEEEET!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM