Hello iFellows; Hope all are enjoying your life. Today, there are  some tips for new iDevelopers. These code chunks will help you out to do some really simple and many-time-usable things. Bring coke and have some light track in iTunes. So here we go; Logging
In Xcode, click Run > Console to see NSLog statements. [sourcecode language="objc"] NSLog(@"log: %@ ", myString); NSLog(@"log: %f ", myFloat); NSLog(@"log: %i ", myInt); [/sourcecode]
Display Images Display an image anywhere on the screen, without using UI Builder. You can use this for other types of views as well. [sourcecode language="objc"] CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f); UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect]; [myImage setImage:[UIImage imageNamed:@"myImage.png"]]; myImage.opaque = YES; // explicitly opaque for performance [self.view addSubview:myImage]; [myImage release]; [/sourcecode]
Application Frame
Use "bounds" instead of "applicationFrame" -- the latter will introduce a 20 pixel empty status bar (unless you want that..)
Web view
A basic UIWebView. [sourcecode language="objc"] CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0); UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame]; [webView setBackgroundColor:[UIColor whiteColor]]; NSString *urlAddress = @"http://www.google.com"; NSURL *url = [NSURL URLWithString:urlAddress]; NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; [webView loadRequest:requestObj]; [self addSubview:webView]; [webView release]; [/sourcecode]
Display the Network Activity Status Indicator
This is the rotating icon displayed on the iPhone status bar in the upper left to indicate there is background network activity taking place. [sourcecode language="objc"] UIApplication* app = [UIApplication sharedApplication]; app.networkActivityIndicatorVisible = YES; // to stop it, set this to NO [/sourcecode]
Animation with Series of images
Show a series of images in succession [sourcecode language="objc"] NSArray *myImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"myImage1.png"], [UIImage imageNamed:@"myImage2.png"], [UIImage imageNamed:@"myImage3.png"], [UIImage imageNamed:@"myImage4.gif"],  nil]; UIImageView *myAnimatedView = [UIImageView alloc]; [myAnimatedView initWithFrame:[self bounds]]; myAnimatedView.animationImages = myImages; myAnimatedView.animationDuration = 0.25; // seconds myAnimatedView.animationRepeatCount = 0; // 0 = loops forever [myAnimatedView startAnimating]; [self addSubview:myAnimatedView]; [myAnimatedView release]; [/sourcecode] Animation: Move an object
Show something moving across the screen. Note: this type of animation is "fire and forget" -- you cannot obtain any information about the objects during animation (such as current position). If you need this information, you will want to animate manually using a Timer and adjusting the x&y coordinates as necessary. [sourcecode language="objc"] CABasicAnimation *theAnimation; theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"]; theAnimation.duration=1; theAnimation.repeatCount=2; theAnimation.autoreverses=YES; theAnimation.fromValue=[NSNumber numberWithFloat:0]; theAnimation.toValue=[NSNumber numberWithFloat:-60]; [view.layer addAnimation:theAnimation forKey:@"animateLayer"]; [/sourcecode] NSString and int The following example displays an integer's value as a text label: [sourcecode language="objc"] currentScoreLabel.text = [NSString stringWithFormat:@"%d", currentScore]; [/sourcecode] Draggable items Here's how to create a simple draggable image. 1. Create a new class that inherits from UIImageView [sourcecode language="objc"] @interface myDraggableImage : UIImageView { } [/sourcecode] 2. In the implementation for this new class, add the 2 methods: [sourcecode language="objc"] - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { // Retrieve the touch point CGPoint pt = [[touches anyObject] locationInView:self]; startLocation = pt; [[self superview] bringSubviewToFront:self]; } - (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { // Move relative to the original touch point CGPoint pt = [[touches anyObject] locationInView:self]; CGRect frame = [self frame]; frame.origin.x += pt.x - startLocation.x; frame.origin.y += pt.y - startLocation.y; [self setFrame:frame]; } [/sourcecode] 3. Now instantiate the new class as you would any other new image and add it to your view [sourcecode language="objc"] dragger = [[myDraggableImage alloc] initWithFrame:myDragRect]; [dragger setImage:[UIImage imageNamed:@"myImage.png"]]; [dragger setUserInteractionEnabled:YES]; [/sourcecode]
Vibration and Sound
Here is how to make the phone vibrate (Note: Vibration does not work in the Simulator, it only works on the device.) [sourcecode language="objc"] AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); [/sourcecode] Sound will work in the Simulator, however some sound (such as looped) has been reported as not working in Simulator or even altogether depending on the audio format. Note there are specific filetypes that must be used (.wav in this example). [sourcecode language="objc"] SystemSoundID pmph; id sndpath = [[NSBundle mainBundle] pathForResource:@"mySound" ofType:@"wav" inDirectory:@"/"]; CFURLRef baseURL = (CFURLRef) [[NSURL alloc] initFileURLWithPath:sndpath]; AudioServicesCreateSystemSoundID (baseURL, &amp;pmph); AudioServicesPlaySystemSound(pmph); [baseURL release]; [/sourcecode] Threading 1. Create the new thread: [sourcecode language="objc"] [NSThread detachNewThreadSelector:@selector(<strong>myMethod</strong>)   toTarget:self withObject:nil]; [/sourcecode] 2. Create the method that is called by the new thread: [sourcecode language="objc"] - (void)<strong>myMethod</strong> { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; *** code that should be run in the new thread goes here *** [pool release]; } [/sourcecode] What if you need to do something to the main thread from inside your new thread (for example, show a loading symbol)? UseperformSelectorOnMainThread. [sourcecode language="objc"] [self performSelectorOnMainThread:@selector(<strong>myMethod</strong>) withObject:nil waitUntilDone:false]; [/sourcecode] Reading crash logs