gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:15:39 25 4
gpt4 key购买 nike

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

我似乎能够正确链接 CSS,但不能正确链接 javascript。我正在尝试使用以下处理 onOrientationChange ( How to build an iPhone website ) 的示例,但我正在从应用程序的 NSBundle mainBundle 提供网页。

我尝试将 javascript 函数附加到 body.onorientationchange 和 window.onorientationchange,但是当从 UIWebView 本地(或远程)提供服务时,这两种函数都不起作用,但如果我使用 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 中收到的警告中找到:

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

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

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

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

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.

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

关于uiwebview - iPhone UIWebView本地资源使用Javascript并处理onorientationChange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/843820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com