Over a million developers have joined DZone.

Universal Launch Images with PhoneGap

DZone's Guide to

Universal Launch Images with PhoneGap

· Mobile Zone
Free Resource

Discover how to focus on operators for Reactive Programming and how they are essential to react to data in your application.  Brought to you in partnership with Wakanda

I've been playing with PhoneGap a lot recently, in part to prepare Marbles2, but also to bring JS Console to the iPhone as a native app. I've always wanted to create an app that worked on all devices, but getting the launch image correct for each device can be tricky.

How the launch image works on PhoneGap

Basically there's two launch images. Once for the native app and once (manually by PhoneGap) whilst the UIWebView (aka Safari) is loading up. The native one is handled by the app as per the documentation on the Apple developer site, but the overlaid image is hardcoded to 'Default.png'. This is what we'll fix.

Changes to PhoneGapDelete.m

To get the three devices to work properly, you need to make a change to PhoneGapDelete.m. Changing the following line:

UIImage* image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Default" ofType:@"png"]];
imageView = [[UIImageView alloc] initWithImage:image];
[image release];


UIImage *image = [UIImage imageNamed:"Default"];
imageView = [[UIImageView alloc] initWithImage:image];
// [image release]

† Note that I've not learnt Objective-c, I've just messed around and Googled enough to get to this point!


The reason we switch from initWithContentsOfFile to imageNamed is for two reasons:

  1. initWithContentsOfFile is also using the bespoke pathForResource which returns the full path to the file, which we don't want as I'll explain.
  2. initWithContentsOfFile apparently doesn't correctly pick up the @2x resources for the iPhone 4 retina displays

Adding the Launch Images

Now you need to create three images, for:

  1. iPhone 3GS and below
  2. iPad
  3. iPhone 4 double resolution

The details of the resolutions are available Apple's developer reference.

The iPhone 4 image is easy, you call it Default@2x.png and drag it in to the xcode project. The iPhone 3GS (and below) image is called Default.png and also dragged in to the project (there will probably already be a phonegap default image that you'll overwrite). Finally the iPad image needs to be called Default~ipad.png.

This only works because we've made the change to use initWithImage because it'll automatically detect the device type and add the extension.

One Final Tip

If you notice that when your app loads the launch image jumps, it's because the phonegap UIWebView is (incorrectly) offset by negative 20 pixels - we can fix this too.

Michael Brooks wrote a really elegant little bit of code that fixes the offset in the web view that's also contributing to the launch image jumping.

Add the code from this gist http://gist.github.com/510407 to your *AppDelegate.m (in that if your app is called jsconsole, add the code to jsconsoleAppDelegate.m).

Once that code is in, you'll have your universal phonegap app with launch images all in the appropriate size for the app.

I used this technique for the iOS version of JS Console (though I'm not totally sure it'll get accepted to the itunes store!!!).

Finally, if you want to learn more about PhoneGap, my conference Full Frontal, is running a full day workshop with Brian LeRoux entirely on PhoneGap, so check it out!

From http://remysharp.com/2010/08/30/universal-launch-images-with-phonegap

Learn how divergent branches can appear in your repository and how to better understand why they are called “branches".  Brought to you in partnership with Wakanda


Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}