iPhone UIWebView 本地资源使用 Javascript 并处理 onorientationChange

iPhone UIWebView local resources using Javascript and handling onorientationChange(iPhone UIWebView 本地资源使用 Javascript 并处理 onorientationChange)

本文介绍了iPhone UIWebView 本地资源使用 Javascript 并处理 onorientationChange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 iPhone 应用程序的本地资源中提供 HTML Javascript 和 CSS 内容,但在处理 onOrientationChange 事件和包含外部 Javascript 时遇到了问题.

I'm trying to server HTML Javascript and CSS content from an iPhone application's local resources, and I'm having trouble handling onOrientationChange events and including external Javascript.

我似乎能够正确链接 CSS,但不能正确链接 javascript.我正在尝试使用以下处理 onOrientationChange 的示例(如何建立一个 iPhone 网站) 但我从我的应用程序的 NSBundle mainBundle 提供网页.

I seem to be able to link in CSS properly but not javascript. I'm trying to use the following example of handling onOrientationChange (How to build an iPhone website) but I'm serving the webpage from my app's NSBundle mainBundle.

我尝试将 javascript 函数附加到 body.onorientationchange 和 window.onorientationchange,但在本地(或远程)从 UIWebView 提供服务时都不起作用,但如果我使用的是 iPhone Safari,它会起作用.

I tried attaching a javascript function to body.onorientationchange and to window.onorientationchange but neither work when served from UIWebView locally (or remotely), but it works if I'm using the iPhone Safari.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>How to build an iPhone website</title>

    <meta name="author" content="will" />
    <meta name="copyright" content="copyright 2008 www.engageinteractive.co.uk" />
    <meta name="description" content="Welcome to engege interactive on the iPhone!" />
    <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;">
    <link rel="apple-touch-icon" href="images/template/engage.png"/>

    <style type="text/css">
        @import url("iphone.css");
    </style>
    <!--
    <script type="text/javascript" src="orientation.js"></script>
    -->
    <script type="text/javascript">


function updateOrientation(){
    try  {
        var contentType = "show_normal";
        switch(window.orientation){
            case 0:
                contentType = "show_normal";
                break;

            case -90:
                contentType = "show_right";
                break;

            case 90:
                contentType = "show_left";
                break;

            case 180:
                contentType = "show_flipped";
                break;
        }

        document.getElementById("page_wrapper").setAttribute("class", contentType);
        //alert('ORIENTATION: ' + contentType);
    }
    catch(e) {
        alert('ERROR:' + e.message);
    }
} 

window.onload = function initialLoad(){
    try {
        loaded();
        updateOrientation();
    }
    catch(e) {
        alert('ERROR:' + e.message);
    }
}

        function loaded() {
            document.getElementById("page_wrapper").style.visibility = "visible";

        }


    </script>

</head>

<body onorientationchange="updateOrientation();">

    <div id="page_wrapper">
        <h1>Engage Interactive</h1>
        <div id="content_left">
            <p>You are now holding your phone to the left</p>
        </div>
        <div id="content_right">
            <p>You are now holding your phone to the right</p>
        </div>
        <div id="content_normal">
            <p>You are now holding your phone upright</p>
        </div>
        <div id="content_flipped">
            <p>This doesn't work yet, but there is a chance apple will enable it at some point, so I've put it in anyway. You would be holding your phone upside down if it did work.</p>
        </div>
    </div>

</body>
</html>

推荐答案

答案可以从我在 XCode 中得到的警告中找到:

The answer can be traced from the warning I was getting in XCode:

警告:没有规则为架构 i386 处理 sourcecode.javascript 类型的文件$(PROJECT_DIR)/html/orientation.js"

warning: no rule to process file '$(PROJECT_DIR)/html/orientation.js' of type sourcecode.javascript for architecture i386

XCode 设置 *.js javascript 作为某种类型的源代码需要在应用程序中编译,当我想将它作为资源包含时,所以我通过做两件事来解决它.

XCode setup *.js javascript as some type of source code needed to be compiled in the application when I wanted to include it as a resource so I solved it by doing 2 things.

  1. 选择 .js 文件并在详细信息"视图中取消选择表示它是编译代码的靶心列
  2. 在组和文件"视图中展开目标"树并展开应用程序,然后转到复制捆绑资源"并将 *.js 文件拖入其中

Apple 开发者论坛发布了解决方案:

The Apple dev forums has a posted solution:

  • UIWebView 和 JavaScript

看来你有点受不了了Xcode 认为 .js 是事物被编译"功能.基本上,Xcode 认为脚本应该以某种方式运行或编译,所以标记它作为源代码的一部分.来源代码当然不会复制到资源.

It appears that you are getting bit by the "Xcode thinks that .js are things to be compiled" feature. Basically, Xcode thinks that the script should somehow be run or compiles, so marks it as part of the source code. Source code, of course, is not copied into the resources.

所以你需要做两件事——选择项目中的 .js 文件,然后打开关闭表示该复选框它被编译(靶心"柱子).如果你不这样做,你会在您的构建日志中收到关于无法编译文件(这应该是你的第一个警告 -总是试图弄清楚和更正任何和所有警告出现在您的构建中).

So you need to do two things - select the .js file in your project, and turn off the checkbox that indicates that it is compiled (the "bullseye" column). If you don't do this, you'll get a warning in your build log about being unable to compile the file (which should be your first warning - always try to figure out and and correct any and all warning that appear in your build).

然后将其拖放到目标的复制捆绑资源".

Then drag it and drop it in the target's "Copy Bundle Resources".

这篇关于iPhone UIWebView 本地资源使用 Javascript 并处理 onorientationChange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:iPhone UIWebView 本地资源使用 Javascript 并处理 onorientationChange

基础教程推荐