BuySellAds.com

Our DNA is written in Objective-C
Jump

Handling Deprecated Methods for iPhone SDK 3.0

In the past week I received an email by Apple, like my dear developer colleagues, stating that I have to consider whether the code of my apps is compatible to iPhone SDK 3.0. Gosh, the guys from Cupertino got me scared!

And that they can’t take a joke occurred this night: The update of my app Super Trumps, submitted about 8 days ago, has been rejected this night. The friendly associate of Apple stated that my app crashes when using it with SDK 3.0. That means: They’re testing apps already now on compatibility!

Without delay I checked the compatibility of all my existing apps and – lo and behold – in two applications problems occurred.

In the lite version of my app Namenstag I’m using an advertisement by AdMob, which isn’t dinky – it carries a yield of at least circa 3 up to 5 bucks a day. Not bad … But the bad thing is the SDK 3.0, because AdMob hasn’t yet finished a stable framework running under SDK 3.0. Using the current framework under SDK 3.0 will cause a crash immediately.

The second problem is called deprecated syntax – even the sound of that is stomach-churning!

The question was: How can I distribute code that runs with SDK 2.0 on the one hand and already with SDK 3.0 on the other hand?

Certainly I thought about an #ifdef immediately. I googled several times with different requests but no satisfying result. Perhaps my request has been unusual, but I haven’t been able to find almost one sufficient source, stating how to differerentiate between old and new SDKs within the code.

First I contacted Dr. Touch, because I always contact him when I’m stuck with a problem … I truly have pity for him because he is always the last resort for me ;-)

His solution was to determine the running OS version during runtime:

NSString *os_verson = [[UIDevice currentDevice] systemVersion];
NSLog(os_verson);

Not bad, indeed, but I’ve been looking for a solution which isn’t only working during runtime but as early as during the build process! It’s because I don’t like these yellow caution labels within Xcode ;-)

Therefore I continued my search and I found something which hasn’t looked bad. With that code snippet I experimented with Xcode code completion and then I found what I was looking for:

#if __IPHONE_3_0
    // Statement for SDK 3.0
#else
    // Statement for older OS versions
#endif

This can be used for example with the old property font for UIButton. font is deprecated within SDK 3.0, one has to use the new titleLabel property for coding style properties of a button:

UIButton *aButton = [[UIButton alloc] initWithFrame:aFrame];
#if __IPHONE_3_0
    aButton.titleLabel.font = [UIFont systemFontOfSize:12.0];
#else
    aButton.font = [UIFont systemFontOfSize:12.0];
#endif

With this switch you can use SDK 3.0 compliant AND compliant code for minor OS versions at the same time.

PS: Finally I’d like to introduce myself: Andreas Heck, a 39 year old German designer, developer and marketing servant, wo has – as an Apple fanatic – begun to develop with Objective-C and Cocoa Touch in the middle of January this year. So far I’ve published 4 applications for the iPhone which one can consider at my official site. Thanks to Oliver Drobnik for his assistance and for inviting me to join his very helpful blog!


Tagged as:

Categories: Recipes

%d bloggers like this: