Posted by & filed under Content - Highlights and Reviews, Programming & Development.

codeA guest post by Rob Lauer, a Product Manager for Telerik who has a passion for hybrid mobile app development and the open web. In his other life he raises two kids, plays Ultimate Frisbee, and (for better or worse) is an avid Wisconsin Badger fan. Rob can be reached at @rdlauer.

If you’ve been following along with my two recent posts on developing for Firefox OS, you may want to start digging a little deeper into the Web APIs. This post is going to quickly cover the basics about Web APIs, how you use them, and which type of Firefox OS app you need to develop to use the Web APIs.

What is a Web API?

In the context of Firefox OS, a Web API describes a means of interacting with a native device feature. Say, for instance, you would like to set an alarm on the device or retrieve the user’s current location. Both of these situations require you to use a Web API to interface with the corresponding device features. Luckily with Firefox OS, this is all handled with plain JavaScript.

Web API Example

To provide a more concrete example, let’s say you want to add a contact to your device’s native contact manager. The JavaScript required to do the actual device interaction is as simple as the following:

There are many other APIs that follow the same conventions. Take a look at Mozilla’s documentation to discover others.

Requesting Permission

Now, you can’t simply write an app and expect your users to allow you to add contacts to their device without first requesting the permission to do so. By continuing to follow the contacts example, you must simply add the following to your Firefox OS app manifest:

For the contacts API, the access property can be readonly, readwrite, readcreate, or createonly. Be sure to read my last post if you are unclear as to how and why you need to create an app manifest.

Which App Type Do I Need?

Each “type” of Firefox OS app (be it hosted, plain packaged, privileged packaged, or certified packaged) has access to a different subset of Web APIs. Thankfully Mozilla has provided us with documentation that shows which APIs require what type of app. From this table you can see that in order to access the contacts API, you must be using a privileged packaged app.

What’s Next?

Hopefully this gives you a better understanding of the Firefox OS Web APIs. I encourage you to take a closer look at all of the APIs as I believe you will start to discover what a powerful and easy to use platform Firefox OS really can be!

Look below for some great JavaScript resources from Safari Books Online.

Not a subscriber? Sign up for a free trial.

Safari Books Online has the content you need

Eloquent JavaScript is a guide to JavaScript that focuses on good programming techniques rather than offering a mish-mash of cut-and-paste effects. The author teaches you how to leverage JavaScript’s grace and precision to write real browser-based applications. The book begins with the fundamentals of programming—variables, control structures, functions, and data structures—then moves on to more complex topics, like object-oriented programming, regular expressions, and browser events. With clear examples and a focus on elegance, Eloquent JavaScript will have you fluent in the language of the web in no time.
Learning JavaScript Programming Video teaches you the basics of programming with JavaScript, the worlds most used programming language. The tutorial is designed for the absolute beginner – no prior JavaScript programming experience is required in order to get the most out of this video training. You will start with learning what programming is, and specifically, what JavaScript is, how it it used, and its limitations. You will discover variables and data types, and how to take input and create output. Craig covers conditions, loops, arrays, sorting, functions, paramaters and debugging. You will even learn advanced concepts such as OOP, string manipulations, regular expressions and other programming patterns.

Tags: Firefox OS, Javascript, Mozilla, permission, Web API,

Comments are closed.