Important Useful Objective-C Snippets for Your iOS Applications - Online Free Computer Tutorials.

'Software Development, Games Development, Mobile Development, iOS Development, Android Development, Window Phone Development. Dot Net, Window Services,WCF Services, Web Services, MVC, MySQL, SQL Server and Oracle Tutorials, Articles and their Resources

Monday, May 21, 2012

Important Useful Objective-C Snippets for Your iOS Applications

Being an iOS developer, you should keep a shed-load of Objective-C snippets nearby so whenever you are coding that you can refer to and copy into your application. There are so many commonly employed elements in an iOS app that it is just about impossible to remember them all. In this article, we will share most important of them. 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.

Lets check them individually, 

OPEN A URL IN SAFARI

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

DIAL A NUMBER

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

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

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

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);

CONFIGURING A UISCROLLVIEW

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

SET TITLE OF A VIEW IN A UIVIEWCONTROLLER

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

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!

I guess you came to this post by searching similar kind of issues in any of the search engine and hope that this resolved your problem. If you find this tips useful, just drop a line below and share the link to others and who knows they might find it useful too. 

Stay tuned to my blogtwitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.

No comments:

Post a Comment