Tuesday, April 17, 2012

When to use CoffeeScript?

Plenty of us are writing applications in Javascript, whether we are writing mobile app code, getting the most of node.js, or just building a website. The CoffeeScript invention came out a few years ago and it really looks great with its promise to make app programming easier.

But we still have to learn Javascript, or at least the good parts, including some things like its functional programming syntax which can get hard to read and oh yeah you have to use === instead of == in a number of cases. CoffeeScript makes these things much easier but if we have to start debugging or using other people's libraries we still have to deal with some of these quirks in Javascript.

And for some rapid development tasks, we can question if it is even worth adding the extra compile step to start using CoffeeScript.

I believe it is worth adding the extra steps to start using CoffeeScript if there are multiple modules or any form of OOP or functional programming are needed. I spent 1-2 months building a mobile application for a client using Javascript modules until I started working on a Cordova/PhoneGap plugin written in CoffeeScript. I started making some changes to its API and really found the CoffeeScript so much easier to manage and refactor. I was able to use js2coffee to convert most of my modules to CoffeeScript and it is so much easier to make the changes and refactor into smaller modules.

This morning I needed a web server that calls a JSON API and returns a certain part of the results. In node.js, there are a couple of levels of callbacks that are needed to handle the asynchronous nature on all sides. I thought about doing it in Javascript and realized that the complexity of handling the callbacks and functional programming can really blow up and it will be much smoother in CoffeeScript. I wrote about how to do the JSON services in node.js here.

The only time I see not to use CoffeeScript is in a community where other people are still using Javascript. I have worked on a Cordova/PhoneGap SQL API and other people have been helping to make some improvements in the Javascript version. In this case, I still want to keep the CoffeeScript version to make refactoring easier but will have to make the Javascript version leading.

Friday, April 13, 2012

Notes on Running Cordova/PhoneGap on Android without Eclipse

NOTE: this information for developing on both OSX & Linux is now given in this entry in my new blog.

Running an old PhoneGap iPhone plugin on Cordova 1.5+ iOS

I have been thinking about the nightmare of supporting multiple versions of Cordova/PhoneGap and had an idea to make a shim to build and run old PhoneGap plugins on Cordova iOS 1.5+. Of course we want the plugins to migrate to the new Cordova API and abandon the old one but this will not happen overnight.

So far I tried making an Objective-C PGPlugin class, deriving from CDVPlugin in PGPlugin.[hm] so that an old PhoneGap plugin can compile and run. Of course we will need some more definitions to support certain plugins but this was a nice quick and easy test. For a first test I discovered phonegap-plugins / iPhone / Prompt which had not yet been ported to Cordova 1.5+.

I put the PGPlugin.[hm] in the Plugins folder, adding a Javascript file, using the test code in onDeviceReady(), and it worked the first time!

I discovered that the extra Javascript to define window.PhoneGap is not necessary before Cordova 2.0, but I checked it in anyway to be prepared for the future.

So I have a start to run old PhoneGap plugins on Cordova 1.5+. Next is to try some more old PhoneGap plugins, especially ones that are exhibiting more dependencies.

UPDATE: test with the BundleFileReader is working great with a typedef and 2 enum values added to PGPlugin.h.

My first Cordova 1.6.0 iOS project: generated by command line

UPDATE: this is no longer relevant, please see the PhoneGap command-line guide for iOS for instructions.

I now have the pleasure to restart my Cordova project based on Cordova 1.6.0. I tried uninstalling Cordova 1.5.0 and then installing Cordova 1.6.0 but my Xcode 4.3.2 decided to use the old template to construct my Cordova project. I got some very nice messages of  built-in "plugins" not being found so I decided to try making my project from the command line.

I found Shazron had already posted  how to create a PhoneGap project from the command line so I made an adaptation (UPDATE: no longer valid) to create a Cordova 1.5/1.6+ project. I am actually very happy with the results since you don't have to drag-and-drop the www folder. You have to make sure you select the right project and target to run the generated project. UPDATE: there will be some ugly log messages in the debug mode so it is better to go into release mode (Apache issue CB-502 has been filed).

Cordova iOS 1.6.0 changed the global Cordova to cordova to be consistent with the Android version, so something like << Cordova ||= cordova; >> has to be added to the Plugin Javascript to work for both Cordova 1.5 and 1.6.

I tried including a couple plugins including an adaptation of the Globalization plugin and chbrody / Cordova-SQLitePlugin and they seemed to be working fine. UPDATE this is no longer valid: Some changes have been checked into https://github.com/chbrody/Cordova-SQLitePlugin/blob/master/Cordova-iOS/Plugins/SQLitePlugin.h, probably due to the different structure generated by create-ios-project.sh.

The next challenge is to run the program on a device. Since the command line did not provide an option to specify the company identifier, simply fix the Bundle identifier on the project summary pane to be like <<MYIDPREFIX.AppName>> and it should work with a wildcard app ID.

Now if you do not want to use the wildcard app identifier you have to go back to the Bundle and take out your ID prefix so the Bundle identifier should be like <<AppName>>. You can also remove your wildcard provisioning profile from the Xcode organizer to make sure it is matching the proper, desired provisioning profile for your app. So the Xcode has stored your ID prefix somewhere, I don't know where but this actually is working for me. If anyone knows how it works please post a comment with a link to a good explanation.

Saturday, March 24, 2012

Phonegap-SQLitePlugin: reliable offline storage for iOS

UPDATE: Please see my new posting: Cordova/PhoneGap sqlite plugins offer large db size, excellent reliability

There has been a bit of discussion on the Cordova/PhoneGap forum that "LocalStorage or SQLite data will no longer be backup and can be deleted" for HTML5 as of iOS 5.1. This news has given me a very bad feeling while I am developing my first iPhone app and I was immediately searching for a more reliable solution. I tried using the Application Preferences plugin but it did not work very well either on the iPhone simulator or on my iPhone device. I discovered the Phonegap-SQLitePlugin which does seem to provide a much more reliable solution.

There has been quite a bit of effort to solve this problem in the Corvdova/PhoneGap project. Kerri Shotts had been kind enough to contribute a workaround that does the backup and restore tasks during startup, "resign active", and termination phases and I did consider using this workaround very seriously. I chose not to use this workaround because first I would have to do separate testing to verify that this is working both pre-iOS 5.1 and iOS 5.1+ and second I have some doubts about its reliability. I am very concerned that if an app or even an iOS device crashes then some user data could be lost. Apache issue CB-330 had been logged and some work had been done in the incubator-cordova-ios project which still uses the backup-and-restore mechanism.

In general, I like the Phonegap-SQLitePlugin much better due to its reliability. Currently this SQLite Plugin works on PhoneGap 1.3 and earlier but not on Cordova 1.5 due to the changes in the iOS plugin API. The SQLite Plugin exposes an API that is similar but not the same as the HTML5 Web SQL API. In general I have been able to find plenty of answers on the Internet about how to format SQLite queries and the PhoneGap SQLite Plugin API should follow the SQLite C API very closely.

In general I am very happy to use the Phonegap-SQLitePlugin. There are a few issues that I would like to see solved in the near future. Cordova support is currently missing and I have had no time to try to make this work. In addition, it would be nice to see an adaptation or even a wrapper to emulate the HTML5 Web SQL API.

Introduction

Hi my name is Chris Brody and I currently busy getting my first iPhone app ready for a client. We are in a fast-growing business and we see so many people trying to get started building mobile apps and mobile web apps. I have started following the Cordova/PhoneGap forum and I see so many people looking for some really basic information so I was inspired to create this blog to post some important pointers.

As I write my first blog post I will give some simple pointers for a "newbie or n00b" considering how to build a mobile app or a mobile web app.

An increasing number of developers are using HTML5 web programming technology which is actually quite advanced. Javascript is actually quite advanced since it provides support for both object-oriented programming and functional programming. I recommend considering CoffeeScript which can make web programming much more easy and fun; Smooth CoffeeScript is an excellent learning resource.

There are a number of wonderful HTML5 programming libraries; my favorites include JQTouchJQuery Mobile, and JQMobi. For mobile web apps you can use the HTML5 app cache manifest to keep the app running offline and the HTML5 local storage or Web SQL API to store user data.

For a mobile app, the first step is to consider what kind of SDK or set of libraries to use. Some more advanced developers may choose to use a native API; however an increasing number of developers are choosing to use portable libraries due to factors such as ease of use, time to market, etc. For HTML5 technology the most commonly used library is Apache Cordova, also known as PhoneGap. I recommend the PhoneGap/Cordova Crib Sheet for anyone looking for resources to help with programming either for an app store or for mobile web apps.

I had been programming mobile messaging systems for +/- 13 years in C and C++ and due to the changes in the marketplace I have decided to make a complete transition to programming mobile apps. I have learned a little bit of Objective-C by adapting certain PhoneGap plugins for iPhone to suit the needs of my iPhone app. I hope this blog will be helpful to those looking for information to get started programming mobile apps.

Chris