Online Book Reader

Home Category

HTML5 Canvas [209]

By Root 6599 0
> 2 || Math.abs(acceleration.y)>2

|| Math.abs(acceleration.z)>2) {

alert('Acceleration X: ' + acceleration.x + '\n' +

'Acceleration Y: ' + acceleration.y + '\n' +

'Acceleration Z: ' + acceleration.z + '\n' +

'Timestamp: ' + acceleration.timestamp + '\n');

stopAccelerationWatch()

chooseButtonsForCard();

drawScreen();

startAccelerationWatch();

}

}

// onError: Failed to get the acceleration

//

function onError() {

alert('onError!');

}

The new code begins with the var accelerationWatchId = null; variable setting. We have created a startAccelerationWatch() function, which will call the onSuccess() function every 100 milliseconds.

The onSuccess() function receives the “acceleration” values from the API, which consist of delta changes to the x, y, and z of the device. These values are set to 0,0,0 when the application begins.

On each 100-millisecond interval, these values are checked. If any is 2 units greater (in either the positive or negative direction) from the 0 beginning values, we fire off an alert and then call our functions to reset the card. First, we call the stopAccelerationWatch() function, which acts as a reset for the location of the device in physical space. Next, we call the chooseButtonsForCard() and drawScreen() functions. Finally, we set up the watch again with startAccelerationWatch().

NOTE

For the most detailed and up-to-date information on integrating PhoneGap with iOS and other device features, visit the PhoneGap documentation website. Most of the information for this example is based off the explanation and examples found at http://docs.phonegap.com/phonegap_accelerometer_accelerometer.md.html.

Testing on a Device


Before we can test the application on an actual device, we need to “provision” the device. To do this, we will need the device, a paid iOS developer account, and a development Mac computer all synced up into a single provisioning profile. This necessary process is not for the faint of heart.

Step 1: Launch the Provisioning Assistant


Begin by visiting the Developer Provisioning Assistant (http://developer.apple.com/iphone) and logging in with your paid iOS developer ID. Once logged in, click on the link for the “iOS Provisioning Portal” (currently in the upper-right side menu). There should be a button called “Launch the Provisioning Assistant.” Click on this button and follow all the on-screen instructions.

Step 2: Find the device’s Unique ID


You will be asked to find the Unique ID of your physical device. This is found by plugging the device into your computer, and then looking up the ID in Xcode. In the Window→Organizer Information window, you will see a list of the attached devices. The ID will be a long alphanumeric string next to the word Identifier.

Step 3: Generate a Certificate Signing Request


When asked by the Provisioning Assistant, input this ID value. You will next be asked to “Generate a Certificate Signing Request”, which involves the Apple Keychain. Follow all the on-screen dialogs carefully because this can be a confusing process. You will need to open the Keychain Access application in the Applications→Utilities folder on your machine, and then follow the on-screen instructions from the Provisioning Assistant.

Step 4: Locate saved Certificate Signing Request File


This will save a Certificate Signing Request File to your desktop. In the next step, the Provisioning Assistant will need to find this file, so it is important to remember where you save the request.

After you find the file, the assistant will ask you to name your profile. This will generate a file that you must download and install on your Mac.

Step 5: Drag provisioning file into Xcode Organizer window


With the location of the downloaded file handy, plug in your device (if it isn’t already plugged in), and open up the Xcode Window→Organizer once again. Simply drag the .mobileProvision file into the Organizer window, and it will set up the profile in Xcode automatically.

Step 6: Set up certificates with the Keychain


Next, you will be asked to download

Return Main Page Previous Page Next Page

®Online Book Reader