22 Useful Objective-C Snippets for Your iOS Apps

&c=” border=”0″ alt=”” />

Advertise here via BSA

As an iOS developer, I keep a shed-load of Objective-C snippets nearby whenever I’m coding that I often refer to and copy into my apps. There are so many commonly employed elements in an iOS app that it is just about impossible to remember them all. In this article, I share my favorites, those that I reach for the most. Some are longer than others, but they are all invaluable to any iPhone or iPad developer.

One great way to make use of these snippets is to assign them to short keywords in an app like TextExpander, speeding up your app development significantly.

Open a URL in Safari

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com/"]];

Dial a Number

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://9662256888"]];

Dial a Number

Dial a Number

Launch Mail and Send an Email

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://mymail@myserver.com"]];

Prevent Sleep Mode

[UIApplication sharedApplication].idleTimerDisabled = YES;

Stop Responding to Touches

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

Resume Responding to Touches

[[UIApplication sharedApplication] endIgnoringInteractionEvents];

Display an Alert Window

UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:@"Warning" message:@"too many alerts" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
[alert show]

Display the Name of your Application

self.title = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];

Change the Style of a Navigation Bar

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];

Get the Current Date and Time

NSCalendar *gregorian = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:[NSDate date]];

Make a Vibration


Display the Number of Launches

NSUserDefaults *countDefaults;
int launchCount;

countDefaults = [NSUserDefaults standardUserDefaults];
launchCount = [countDefaults integerForKey:@"launchCount" ] + 1;
[countDefaults setInteger:launchCount forKey:@"launchCount"];
[countDefaults synchronize];

NSLog(@"Launch number: %i", launchCount);

Console Log of Launch Number

Console Log of Launch Number

Configuring a UIScrollView

[scrollView setScrollEnabled:YES];
[scrollView setContentSize:CGSizeMake(320, 1400)];

Set Title of a View in a UIViewController

viewController.title = @"Title Here...";

Navigation Bar Title

Navigation Bar Title

Set the Font and Size of a UITextView

[thankYouTextView setFont:[UIFont fontWithName:@"Helvetica" size:16]];

Set the Font Size of a UILabel

[textView setFont:[UIFont fontWithName:@"Helvetica" size:16]];

Presenting a UIViewController

ViewController *viewController = [[ViewController alloc] init];
viewController = @"Title Here...";
[self.navigationController viewController animated:YES];

Hiding the Back Button in a Navigation Controller

self.navigationItem.hidesBackButton = YES;

Set Background of View

self.view.backgroundColor = [UIColor colorWithHue:1.0 saturation:0.0 brightness:0.6 alpha:1.0];

Give App Icon a Badge Number

[UIApplication sharedApplication].applicationIconBadgeNumber = 10;

Align UILabel Text

titleLabel.textAlignment = UITextAlignmentCenter;

Set Background of Tab Bar

UIImage *tabBarBackground = [UIImage imageNamed:@"TabBarOverlay.png"];
[rootController.tabBar setBackgroundImage:tabBarBackground];

Have you got some favorite snippets you find yourself reaching for constantly that haven’t been included on the list? Tell us about them in the comments!

Fuel Your Coding

Leave a Comment