At Outbox we have been hard at work building our iPad application withPhoneGap. I wanted to share some of the lessons my team has learned so far. All of the following tips have been tested on iOS 4, 5, and 6 with PhoneGap 1.8. Also note that these tips apply just as well to web applications running on mobile Safari.
Want more information about developing high quality PhoneGap applications, with in-depth explanations and working code examples? Check out phonegap-tips.com.
Tip #1 – Test on old hardware
Your app looks and feels great on that shiny new iPad 3 or iPhone 4S, right? Unfortunately, not everyone runs out to drop $500 whenever Apple releases a new toy. Get your hands on a first generation iPad or third generation iPhone. Always test on them first. If your app performs acceptably on on old hardware it is going to be blazing fast on the newer hardware.
Tip #2 – Use pre-emptive 3D transforms
If an element is going to be transformed in response to user interaction put an identity 3D transform on the element before any user interaction happens, for example:
When the user first interacts with the element the initial movement will be much smoother.
Tip #3 – Show or hide elements with 3D transforms when possible
Invariably you will have some elements that need to be shown or hidden based on user interaction. For example, a modal dialog or menu of some sort. The easiest way to do this is usually display:none or visibility:hidden, however, you can achieve a significant speed increase by using a 3D transform to position the element offscreen. In my experience, using this technique to show or hide menus resulted in a 3-5X speed increase over display:none.
Tip #4 – Preload images
Even though your images are already stored on the iOS device there will still be a noticeable “pop-in” when the images are first displayed. I found CSS3 caching to be the best method for dealing with this issue. If you are not keen on enumerating and maintaining a list of every background image you use in your app, here is ajQuery plugin that will preload any background image referenced in your CSS.
Tip #5 – Phark image replacement does not play nice with 3D transforms
Image replacement by setting a large, negative text-indent is great for accessibility since screen readers will find the text and browsers will only display the image. However, the text-indent will make for unbearably choppy 3D transitions in mobile Safari (this is actually related to tip #7). Fortunately, there are alternatives to Phark that perform much better.
Tip #6 – Velocity scrolling!
Velocity scrolling is a super easy way to get a native feel in the scrollable portions of your app. It is simple to enable it on any scrollable area with a CSS rule:
Tip #7 – Respect maximum texture sizes
Each iOS device has a maximum texture size. If you try to smoothly transform an element larger than the maximum texture size, the element will be broken into smaller tiles before the transform is applied, and this a very slow and ugly process.
Tip #8 – Hide large, off-screen images
If your app displays many large images, hide the images that are off-screen. Otherwise, it doesn’t take many large images to unceremoniously crash your app. Both display:none and visibility:hidden work just fine, but tip #3 will not cut it here.
Tip #9 – Eliminate tap event delays
It takes mobile Safari about a third of a second (300 milliseconds) to decide that a touch start event followed by a touch end event should be synthesized into a click event. Plenty of frameworks, such as jQuery Mobile, have “tap” handling code that can eliminate this delay for you.
Tip #10 – Disable tap highlighting
Overall we’re loving PhoneGap at Outbox. Hopefully these tips will help make a few more happy PhoneGappers!