Phonegap - 第一个应用程序

时间:2013-01-14 23:00:49

标签: ios xcode cordova

我正在使用Phonegap for iOS构建我的第一个应用程序。我试图获取加速度计值并将其显示为屏幕上的警报。我有最新版本的cordova-2.3.0。这是index.html文件的代码 -

<!DOCTYPE html>
<html>
    <head>
        <title>PhoneGap Device Ready Example</title>
        <script type=”text/javascript” charset=”utf-8” src=”cordova-2.3.0.js”></script>
        <script type=”text/javascript” charset=”utf-8”>
            document.addEventListener(“deviceready”, onDeviceReady, false);

            function onDeviceReady() {
                navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);
            }

            // onSuccess: Get a snapshot of the current acceleration
            //
            function onSuccess(acceleration) {
                alert('Acceleration X: ' + acceleration.x + '\n' +
                      'Acceleration Y: ' + acceleration.y + '\n' +
                      'Acceleration Z: ' + acceleration.z + '\n' +
                      'Timestamp: '      + acceleration.timestamp + '\n');
            }

            // onError: Failed to get the acceleration
            //
            function onError() {
                alert('onError!');
            }
            </script>
    </head>
    <body>
        <h1> Example </h1>
        <p>getCurrentAcceleration </p>
    </body
</html>

然而,没有任何反应。我得到一个白色的屏幕,上面写着Example和getCurrentAcceleration。

有人可以帮忙吗?

由于

1 个答案:

答案 0 :(得分:1)

  1. 我不知道你的问题是否存在,但你错过了身体末端的标签,看起来像 </body.

  2. 确保您的config.xml文件中包含以下行:

    <plugin name="Accelerometer" value="CDVAccelerometer" />

  3. 还有其他一些手机功能,例如navigator.notification.alert吗?也许你的项目设置不正确?

  4. 确保cordova-2.3.0.js适用于iOS版本。