使用Javascript和处理onorientationChange的iPhone UIWebView本地资源 [英] iPhone UIWebView local resources using Javascript and handling onorientationChange

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

问题描述

我正在尝试从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:


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

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

XCode setup * .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. 在Groups& files视图中展开Targets树并展开应用程序,然后转到Copy Bundle Resources并将* .js文件拖到它

Apple dev论坛有一个已发布的解决方案:

The Apple dev forums has a posted solution:

  • UIWebView and 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文件,并从
的复选框中转出
,表示编译了
(bullseye
列)。如果你不这样做,你将
在你的构建日志中收到关于
无法编译文件
的警告(这应该是你的第一个警告 -
总是尝试找出并且
更正你的构建中出现
的所有警告。

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).

然后将其拖放到$中b $ b target的Copy Bundle Resources。

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆