Archive for December, 2008

Some cool tips and tricks for the iPhone SDK

December 21st, 2008

During the course of developing my latest iPhone application (currently awaiting approval from Apple) I solved a number of issues I’ve had in previous attempts but I’ve finally solved them.  Apologies to anyone reading this that thinks, derrr I’ve known this for ages, but hopefully it’ll help some developers with their iPhone applications.

1) Updating the main UI from a thread.

If you call a function using a thread

[NSThread detachNewThreadSelector:@selector(getXML) toTarget:self withObject:nil];

then you might want to update the main user interface e.g. to display a status on a toolbar.  Well as it turns out the thread is protected and can’t access the main ui whilst the thread is running.  The way to solve this is using the following code

[self performSelectorOnMainThread: @selector(updateBadge) withObject:nil waitUntilDone:NO];

where updateBadge is a function.

2) Changing a refresh button to display a UIActivityIndicatorView on a toolbar
Let’s say we have a toolbar at the bottom of the screen and we’ve added a refresh button e.g.
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(actionRefresh:)];

refreshItem.style = UIBarButtonItemStylePlain;

NSArray *items = [NSArray arrayWithObjects: flexItem, refreshItem, nil];

[toolbar setItems:items animated:NO];

Then by default with that code it would display a refresh button at the right hand side of the toolbar, and when clicking on it would call refreshAction:(id)sender.  To be helpful to the end user we might want to change the refresh button to an activity indicator whilst the refresh action was taking place.

The first job is to make the refresh button and flex item a global property so you can call them from different place.  Now we also need to create a UIActivityIndicator.

When the refresh action starts you can call the following code to display the activity indicator

CGRect frame = CGRectMake(0.0, 0.0, 25.0, 25.0);

UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc] initWithFrame:frame];

[loading sizeToFit];

loading.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);

[loading startAnimating];

UIBarButtonItem *statusInd = [[UIBarButtonItem alloc] initWithCustomView:loading];

statusInd.style = UIBarButtonItemStylePlain;

NSArray *items = [NSArray arrayWithObjects: flexItem, statusInd, nil];

[toolbar setItems:items animated:NO];

Then when the refresh action stops, simply call the following, as we’ve already made flexItem and refreshItem a global property we don’t need to redefine them or re-point them to their action selector.

NSArray *items = [NSArray arrayWithObjects: flexItem, refreshItem, nil];

[toolbar setItems:items animated:NO];
3) How to display an application badge
Now of the three items I discovered this is probably the easiest and it simply the code you need to display the little red badge icon, like the one you see when you have unread email on the mail icon on the home screen.
[UIApplication sharedApplication].applicationIconBadgeNumber = 10;
To hide the badge simply set the value to 0

The dilemma of iPhone Apps

December 6th, 2008

I’m sure other iPhone developers have come up against the same dilemma as me, you come up with what you think is a great idea for a new iPhone Application to perform a quick search on the iTunes App Store and discover that 5 other people have done the same thing.  So the dilemma, do you still write the app because you think you can do better, or do you think of another idea?

Well so far I’ve developed one that I came up with the idea and the same day it got published two others of the same subject did.  For the second idea no one had written an app on this subject yet.  Yet when I came to submit the final code there were three, yet I still submitted because I’d spend enough time working on it.

Now the second dilemma, I’ve got another idea, which obviously I won’t share until I’ve completed it, but do I actually bother as at the time of writing, no one has done this yet, but will they have done by the time I finish coding the application.  Although it will probably only take a fortnight to code.

Heck I’ll probably write it anyway… watch this space!

The simply amazing MacBook Air

December 6th, 2008

Unlike many teccys, I’m neither in the Windows, Mac or Linux camp.  I’m actually in all three.  I love using Linux for web servers, they are simply the best operating system in my opinion for running PHP, MySQL and Apache (otherwise known as LAMP).

I also love Windows Vista.  Controversial some might say, however I have NEVER had a single issue with Vista and it works fantastically for me, it looks great as well!

Now onto the Apple Macs.  My history with macs is both relatively recent and actually quite old at the same time.  I can remember as a kid, my dad bringing one of the early Macs home from work and me playing with the drawing tools.

Now my first mac was an iMac Snow, one of the old CRT screens all in one with a CD drive, running OS X 10.2, then came an iBook G4, followed by a Mac Mini.

Then came the current iMac generation which interestingly also makes my most stable Windows PC to date due to the fantastic BootCamp.

Just before I went on holiday this year i finished my latest iPhone App, called iSki, which shows the snow fall and webcams for all the major ski resorts in the world.  I submitted it to the App Store the day before I left, knowing that it usually takes Apple about a week to two to approve initial submissions of new Apps.

At the end of the second week, the App was rejected due to minor issues and I knew could be fixed in seconds and resubmitted.  

How does this relate to the MacBook Air? Well if I had a Mac with me I could have fixed this issue, resubmitted within minutes rather than waiting a week to get home and then a further week to get it approved (which actually it was).

So the answer for me was either the MacBook or the MacBook Air?  The MacBook is cheaper and more powerful but the MacBook Air is incredibly small and very light.  I almost got a MacBook until I remembered the entire reason for the purchase… portability!  Then I remembered the Apple Refurb Store.

The rest is history, I’m now typing this on my Air and it’s a wonderful bit of kit, if I’d have wanted a desktop replacement I’d have gone for the MacBook Pro, for the portable work system, the Air wins on so many levels for me.