16.11.2015 Views

PG2

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Testing and Debugging PhoneGap Applications<br />

39<br />

function onDeviceReady() {<br />

//Just writing some console messages<br />

console.warn("This is a warning message!");<br />

console.log("This is a log message!");<br />

console.error("And this is an error message!");<br />

}<br />

<br />

<br />

<br />

Debug Example<br />

Look at the console to see the messages the application<br />

has outputted<br />

<br />

<br />

As you can see from the code, all the application has to do is call the appropriate<br />

method and pass in the text of the message that is supposed to be written to the<br />

console.<br />

In some cases, the browser component executing your application’s web content<br />

won’t throw an error if you try to do something that’s not supported in your Java-<br />

Script code (calling a PhoneGap API function that doesn’t exist, for example,<br />

because you’ve misspelled it). In this scenario, simply wrap the errant call in a<br />

try/catch block so your application will have a chance to write its error to the<br />

console, as shown in the following example:<br />

try {<br />

console.log("Validating the meaning of life");<br />

somefunctioncall("42");<br />

} catch (e) {<br />

console.error("Hmmm, not sure why this happened here: " +<br />

e.message);<br />

}<br />

Figure 2-6 shows the messages from Example 2-1 highlighted in the Xcode console<br />

window. This window is accessible while the program is running on an iOS<br />

simulator, so you can debug applications in real time.<br />

Figure 2-6 ​ ​Viewing console messages in Xcode

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!