Documente Academic
Documente Profesional
Documente Cultură
This is noteworthy as this function is responsible for executing the majority of commands that we'll issue although we would rarely ever really notice it. This function is pretty self explanatory and the superb example of documentation/comments above it further acts as a form of explanation. By taking the arguments and pushing them to the end of the queue, it then waits for PhoneGap.queue.timer- an object that is interfaced with directly from the Objective-C code. (Something you'll see later in Part 2!) The comments above hint at the function only being ran when PhoneGap is correctly set- up and ready to receive
This code clearly checks that the state of the web page is either loaded or complete and that the DeviceInfo.uuid (The UUID of the device) object holds a valid value. If these conditions are true, it will run a function that is specified as an argument. (i.e. specified as an anonymous function in the call) The last function we are going to look at in this file is the rather important one that explains the exact method of which JavaScript code calls the objective-C. This is by a rather cool trick that sets the location of the page to a custom protocol. Here it is! PhoneGapLib/javascripts/core/phonegab.js.base:
/** * Internal function used to dispatch the request to PhoneGap. It processes the * command queue and executes the next command on the list. If one of the * arguments is a JavaScript object, it will be passed on the QueryString of the * url, which will be turned into a dictionary on the other end. * @private */ PhoneGap.run_command = function() { /* snip */ var uri = []; var dict = null; for (var i = 1; i < args.length; i++) { var arg = args[i]; if (arg == undefined || arg == null) arg = ''; if (typeof(arg) == 'object') dict = arg; else uri.push(encodeURIComponent(arg)); } var url = "gap://" + args[0] + "/" + uri.join("/"); if (dict != null) { var query_args = []; for (var name in dict) { if (typeof(name) != 'string') continue; query_args.push(encodeURIComponent(name) + "=" + encodeURIComponent(dict[name])); } if (query_args.length > 0) url += "?" + query_args.join("&"); } document.location = url; };
This is the real brain of the framework. This block of code takes all the arguments and formats them into a string that is compliant like a standard URL. Well cover how the arguments are handled in the native code in the next part, but what we need to know now is the format of the URL it generates. Remember: this URL scheme is used behind the scenes for every call to the underlying native framework.
So to run an alert, which is a method of the Notification commandhandler (PhoneGapCommand sub-class), the generated URL sent to the UIWebView would be: gap://Notification.alert?message=Called%20By%20PhoneGap&title=Hello&buttonLabel=Goodbye This would call the alert method, of the Notification Command Handler and provide 3 arguments; the message, title and button label. Its important to remember that these URLs are generated through the JavaScript component of the framework, and most PhoneGap developers will be unaware of the functioning under the hood. Instead, a developer will call this functionality like this:
navigator.notification.alert( 'Called by PhoneGap', function(){ /* callback function */}, 'Hello', 'Goodbye' );
So now we have a basic understanding of what the JavaScript does when it executes a command, and weve also seen an example of how a developer would call such a command, heres a look at one of the JavaScript objects responsible for a command; the wrapper for the native compass functionality. PhoneGapLib/javascripts/core/compass.js
Compass.prototype.start = function(args) { PhoneGap.exec("Location.startHeading", args); }; Compass.prototype.stop = function() { PhoneGap.exec("Location.stopHeading"); };
Using our previous knowledge of the PhoneGap.exec() method, we can understand that we're simply telling PhoneGap to start utilising the Location service in one function, and telling it to stop in the next. The only difference being the optional second argument in the first call to PhoneGap.exec this is named in a self explanatory fashion; and contains the arguments to the call. So now we have a pretty thorough understanding of the JavaScript part of the PhoneGap Framework. In Part 2 we will explore the bindings of the native implementation examining how it interfaces with the native SDK.