iOS

Creating your own Singletons in Objective-C

No Comments

Singletons can be considered like a global class instance that can be accessed by any other class in the program. This is particularly useful if you have certain states or methods that can be grouped up into a single class and allowing other classes to make necessary calls to it. This practice is quite widely used by Apple in their frameworks such as [UIApplication sharedApplication] and [NSNotificationCenter defaultCenter] etc.

We start by creating a new class subclassing NSObject (or other classes that you want) and add a class function to access this singleton.

In the .h,

@interface APIClient : NSObject

+ (APIClient *)sharedInstance;

@end

After that, we implement the method to access the singleton in .m,

#import "APIClient.h"

@implementation APIClient

+ (id)sharedInstance {
    static APIClient *__sharedInstance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        __sharedInstance = [[APIClient alloc] init]:
    });
    
    return __sharedInstance;
}

//...

@end

That’s it! Now all you have to do is to do a #import “APIClient.h” in other classes that you want to use it.
And called the class method [APIClient sharedInstance] to get the single instance throughout your application!

General

New Look!

No Comments

If you’ve noticed recently, I have updated CodeTuition’s look.
Look forward to more tutorials in very near future.

Thanks for all the support that was given so far =)

iOS

Supporting iPhone 5 and iOS 6 for your apps

No Comments

So iPhone 5 has been announced. What does it mean to you as an iOS developer?

Tools

First step is to update your tools, you’ll need to download the latest XCode 4.5 GM (look for iOS6 section) which at the moment is available from https://developer.apple.com/.

Install it, and you’ll have iOS6 SDK right within the XCode.

Supporting 4 inch of iPhone 5 screen dimensions

If you don’t support the new screen size, your apps will just contain black blank areas which isn’t very nice. This also depends if your previous apps were done in very resizable manner (eg. UITableViews etc) which can immediately stretch out to support longer listings.

So add support for the new 568h resolution, run XCode, under Targets (pick your app) and proceed to the Summary Tab. You’ll see that under launch images, there is a Retina (4-inch) section. All you got to do is to import a 640×1136 launch image and your app is instantly supporting iPhone 5 resolution.

That’s not over. You’ll need need to edit your views to be resizable by height and also anchor them based on your preference.