【问题标题】:How do I display a local html file in a UIWebView?如何在 UIWebView 中显示本地 html 文件?
【发布时间】:2010-03-15 21:28:17
【问题描述】:

我有一个相对简单的问题,我似乎找不到答案。在学习 Google Maps Java API 教程时,我遇到了一个问题。我可以从 Web 加载 HTML 文件,但是当我在本地尝试时,它只是显示文件的内容而不是运行脚本。

以下是有效的:

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

我想在本地存储 HTML 文件并从设备本身运行它,所以我尝试了:

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

它只是显示了文件的内容。

这是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>

我在这里做错了什么?

托马斯

【问题讨论】:

  • 您好 Thomas,您清理了哪些目标?我有大约 12 个带有 javascript 的 html 页面,我想在手机上显示它们。
  • 我清理了所有目标。构建
  • 谢谢,非常感谢。我会尽力。我正在制作一本免费的离线俄语圣经,有 1200 页和 250 页供儿童阅读。我需要了解更多。已经花了几天时间,还没有弄清楚如何让外部 .js 执行。在这个网站上找到了一些指南,希望对您有所帮助。再次感谢您的回答。

标签: html iphone xcode uiwebview google-maps


【解决方案1】:

好的。我清理了所有目标,现在它可以工作了...SWEEEET!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-22
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-10
    • 1970-01-01
    相关资源
    最近更新 更多