How to Show the Network Activity Indicator

Dec 16, 2009   //   by Derek van Vliet   //   Uncategorized  //  1 Comment

Screen shot 2009-12-16 at 10.14.26 PMThe network activity indicator is like the UIActivityIndicatorView we previously discussed, only it sits on the status bar, it is smaller, and believe it or not, it is even easier to manipulate. It’s the little rotating wheel of bars (pictured right) that shows up on the status bar whenever your iPhone is accessing the network.

Show It

If you would like to let your users know that your iPhone app is currently swapping data with the network, you can do so with this simple line of code:

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

Hide It

You guessed it: once you’re done showing it, to hide it again, just set the same UIApplication property to NO like so.

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

How to Make the iPhone Vibrate

Dec 15, 2009   //   by Derek van Vliet   //   Development  //  1 Comment

The iPhone is capable of dazzling the user eyes by pushing impressive graphics around its screen and tickling their ears with a lot of audio to go with it, but you may want to give users of your iPhone app some additional sensory experience on top of that. You can do this by making the device vibrate.

We actually use the Audio framework to vibrate device. Here is how to do it in 3 simple steps:

1. Add the AudioToolbox framework to your target.

2. In the file you intend to trigger a vibration, import the AudioToolbox header file:

#import <AudioToolbox/AudioToolbox.h>

3. Finally, call the following line to make the device vibrate:


How to Retrieve Your App Delegate Singleton Instance

Dec 14, 2009   //   by Derek van Vliet   //   Development  //  3 Comments

You should be familiar with the application delegate class if you have ever developed any kind of iPhone app. It is the class that contains your app’s applicationDidFinishLaunching which most if not all apps use as an entry point for execution. You might use it to keep track of application-level state variables or objects, for example.

As such, from time to time you will have a need to retrieve your app’s application delegate instance. You can retrieve it from anywhere within your app by running the following single line of code:

MyAppDelegateClass *app = (MyAppDelegateClass *)[[UIApplication sharedApplication] delegate];

Just be sure to replace “MyAppDelegateClass” with your own iPhone app’s application delegate class name.

How to Get Your App’s Display Name and Version

Dec 13, 2009   //   by Derek van Vliet   //   Development  //  3 Comments

Have you ever needed to retrieve your iPhone app’s name or version at runtime? Sure, you can use constants in a lot of cases, but it can often save you time in the long run to make use of the information that is available via a class called NSBundle.

The Code

You can retrieve your iPhone app’s display name using the following one line of code:

NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];

To retrieve your iPhone app’s version number, use the following similar line of code:

NSString *appVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];

Use Case

Screen shot 2009-12-13 at 12.41.07 PMIn Addicus, when you click the “Get Set” button on the main menu, it links to our website and passes in the referring application name and app version as querystring parameters, allowing us to track visits to our website from different versions of all of our iPhone games. To do this, we combine the above code with code that opens a URL. Here is the resulting code:

NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
NSString *appVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
NSString *url = [NSString stringWithFormat:@"",appName,appVersion];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

Since we’re retrieving this info via the NSBundle class, we never need to change this code when we update the version of the app. The version change propagates down to the URL we open automatically. And this code can be reused in any app without changing it.

How to Dial a Phone Number

Dec 12, 2009   //   by Derek van Vliet   //   Development  //  3 Comments

photo-4With so many awesome games and apps on the iPhone, it is easy to forget that it can actually be used to make phone calls. No, really, it’s true! And as is the case with so many other pieces of functionality, this is easy to accomplish with the iPhone SDK.

Like other phones, the iPhone supports the tel application protocol in URLs. This means that all we need to use is the trusty openURL method of the UIApplication class. We have previously discussed this method when pre-composing emails and, believe it or not, opening a URL.

The Code

Here is how to dial a phone number from your iPhone app in one line of code:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:555-555-5555"]];

Dialing From a Web Page

You can also put links in your web pages that dial a phone number on the iPhone by using the tel protocol like so:

<a href="tel:555-555-5555">Dial 555-555-5555</a>

How to Hide (and Show) the Status Bar

Dec 11, 2009   //   by Derek van Vliet   //   Development  //  No Comments

On the iPhone’s 320×480 screen, pixel real estate is at a premium, especially for games. Often, the status bar represents 20 pixels of height that you as a developer just can’t afford to give away. Fortunately there are 2 simple ways to remove the status bar.


uistatusbarhidden2The first way is by adding a setting to your app’s info.plist file called UIStatusBarHidden. This is ideal for apps that should never display the status bar. Just follow these steps to change the setting:

1. Open your iPhone app’s info.plist file.

2. Command-click and select Add Row.

3. Select “Status bar is initially hidden” from the drop down that appears.

4. Check the checkbox that appears next to the new row.

You could also add this setting to your app’s info.plist file by opening it in a text editor and adding the following 2 XML tags inside the <dict> tag:


How to Do it With Code

If you need the status bar to appear and disappear at runtime, then you will need to use code to do so. You can hide the status bar with one line of code:

[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];

And to show it again, simply call the above line and pass “NO” into setStatusBarHidden instead of “YES”. You can also fade the status bar in and out by passing “YES” into the “animated” part of the message.

String Comparison Using NSString

Dec 10, 2009   //   by Derek van Vliet   //   Development  //  3 Comments


One of the most common things a programmer needs to learn how to do when faced with a new API is how to compare one string to another to see if the two are the same or how they differ.

Because of this, many APIs have created some handy string comparison features, and the NSString class in objective-c is no exception. What follows are some invaluable tools for string comparison in objective-c.

The Is-Equal Method

The NSString class responds to a message and returns whether or not the NSString you pass in is equal or not:

NSString * str = @"oranges";
BOOL res = [str isEqualToString:@"apples"];

The Compare Method

The NSString class also has a method called compare, which gives you a bit more info in the return results. It can actually tell you the difference between the two strings; whether or not the string you passed in is:

  • the same,
  • in ascending sort order to the string you call the message on or
  • in descending sort order to the string you call the message on
NSString * str = @"oranges";
NSComparisonResult res = [str compare:@"apples"];
switch (res) {
	case NSOrderedAscending:
		// going up
	case NSOrderedSame:
		// even steven
	case NSOrderedDescending:
		// down i go

Insensitivity to Case

Finally, the NSString class also features a method for comparing strings without case sensitivity. This is useful for comparing strings with unpredictable case, such as user input. Like the compare method, it returns an NSComparisonResult enumerated value which allows you to see the equality or sort-order of the two strings.

NSString * str = @"APPLES";
NSComparisonResult res = [str caseInsensitiveCompare:@"apples"];
switch (res) {
	case NSOrderedAscending:
		// onward and upward
	case NSOrderedSame:
		// same old
	case NSOrderedDescending:
		// downfall

How to Set Your App’s Splash Screen

Dec 9, 2009   //   by Derek van Vliet   //   Development  //  2 Comments

All iPhone apps have a splash screen, or what Apple refers to as a “launch image”. It is the screen that is displayed immediately after you press your app’s icon on the home screen, while the app icons are sweeping away and your app is zooming into view.

Some apps opt not to display a splash screen and go for a black screen, which is the default behaviour when you create an app. Others display a wireframe of the app’s interface in order to look like it is loading faster. See Apple’s native apps such as Clock and Camera for good examples of this. The most common use of the splash screen (especially in games) is to present a company or game logo, as we do in Addicus:


How to Do It

Apple has made it so easy to set your splash screen that you don’t even need a single line of code to do it. Why, you don’t even need to change a setting. Here’s how to set your splash screen it in just 2 steps:

1. Add a file to your project’s Resource folder called Default.png.

2. There is no step 2. Take this time to reflect on how good life is.

And that’s it. Run your app and your splash screen will zoom into glorious view.

Heads Up

A couple of things to watch out for when working with splash screens:

Whatever image you give it will be scaled to fill the 320×480 resolution of the iPhone, so ideally you would use a 320×480-sized image.

If your iPhone app is running in landscape mode, you need to rotate the splash screen you use. For example, our splash screen is rotated 90 degrees to the right in the above image.

iTunes Connect Unavailable to Developers Dec 23 – Dec 28, 2009

Dec 8, 2009   //   by Derek van Vliet   //   PSA  //  No Comments

news_calendarAllow us to take a break from our adventures in iPhone development to give you a brief service announcement.

Apple just announced that iTunes Connect, the site that developers use to submit and manage their iPhone apps, will be unavailable from December 23 – December 28, 2009. Quote:

iTunes Connect, the tool you use to manage your applications and access your reports, will be unavailable from December 23 through December 28, 2009.

Access to iTunes Connect will resume December 29, 2009.

This means a couple of things:

1. Developers will be flying blind for 6 days. They will not be able to access their sales data, crash logs or financial reports until Dec. 29.

2. This also means no new apps or updates to apps can be submitted to Apple during this time. Developers may have to adjust their schedules accordingly.

3. Users who were hoping that an app will go on sale for Christmas day are most likely out of luck since developers will not be able to change their prices during that time. Unless developers are prepared to hold a 6 day sale, don’t expect many deals on apps after you open your iPhone on Christmas morning.

We now return to your regularly scheduled iPhone dev blog.

How to Get the Platform Name

Dec 8, 2009   //   by Derek van Vliet   //   Development  //  1 Comment

With each iPhone OS-powered device that Apple has added to its lineup, along with it has come new capabilities and new APIs. Because of this, it is often necessary for an app to figure out what platform it is running on before deciding to enable device-specific functionality, such as functionality that would use the compass on the iPhone 3GS, vibration on iPhones or GPS on iPhones.

Here is a function that serves just that purpose by returning the internal name of the platform:

#include <sys/types.h>
#include <sys/sysctl.h>
-(NSString*)getPlatformName {
	size_t size;  
	sysctlbyname("hw.machine", NULL, &size, NULL, 0);  
	char *machine = (char*)malloc(size);  
	sysctlbyname("hw.machine", machine, &size, NULL, 0);  
	NSString *platform = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding];
	return platform;

Possible Values

The possible values that the above function will return when developing for the iPhone OS are listed below along with the product they represent.

Return Value Product
i386 iPhone Simulator
x86_64 iPhone Simulator
iPhone1,1 iPhone 1G
iPhone1,2 iPhone 3G
iPhone2,1 iPhone 3GS
iPhone3,1 iPhone ?
iPod1,1 iPod Touch 1G
iPod2,1 iPod Touch 2G

Example Usage

The following is an example of an if statement that takes advantage of the results of the above function.

NSString * platform = [self getPlatformName];
if ([platform isEqualToString:@"i386"]) {
	// do simulator-specific stuff, perhaps enable a more verbose level of logging?
else if ([platform isEqualToString:@"iPod2,1"]) {		
	// do iPod-specific stuff, perhaps disable UI that refers to GPS, vibration, etc.
else if ([platform isEqualToString:@"iPhone2,1"]) {
	// do iPhone 3GS-specific stuff, perhaps compass related?

Our Games

Latest Tweets