iPhone

iPhone: Launching iTunes links from UIWebView

Monday, December 21st, 2009

Add the following code to your UIWebViewDelegate:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        [[UIApplication sharedApplication] openURL:request.URL];
        return false;
    }
    return true;
}

iPhone: How to make iPhone vibrate

Friday, December 11th, 2009

Add ‘AudioToolBox’ framework

//import the necessary files
#import 

// Vibrates the phone
- (void)vibratePhone {
  AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
}

iPhone: Change the app name under icon.

Tuesday, December 1st, 2009

This one is easy to. Change the ‘Bundle display name’ value in the Info.plist file.

iPhone: Custom Fonts Using Zynga's FontLabel Class

Monday, November 30th, 2009

Using custom fonts is not the most straight forward thing on the iPhone. Luckily good people at Zynga developed an easy to use FontLabel class. Download it here. Add the files to your project.

Step 1. Import the FontLabel header:

#import "FontLabel.h"

Step 2. Create a label.

//note the name of my font file is 'Rage italic.tty'
FontLabel *label = [[FontLabel alloc] initWithFrame:CGRectMake(10, 10, 210, 110) fontName:@"Rage Italic" pointSize:30.0f];

//set the color
label.textColor = [UIColor colorWithRed:0.254 green:0.216 blue:0.078 alpha:1];

//set the text
label.text = @"Denis you're a handsome man!";

//skip the background. make it transparent
label.backgroundColor = nil;
label.opaque = NO;

label.lineBreakMode = UILineBreakModeTailTruncation;

//dynamic number of lines
label.numberOfLines = 0;

//text alignment
label.textAlignment = UITextAlignmentCenter;

//add to where you need it
[myView addSubview: label];

//and release it
[label release];

iPhone: Rotate an UIImageView

Monday, November 30th, 2009

Couldn’t be easier. Just make sure that degrees are in radians.

imgView.transform = CGAffineTransformMakeRotation (degrees);

You can spice it up with animation:

[UIView beginAnimations:@"rotateAni" context:nil];
//set the duration to 5 seconds
[UIView setAnimationDuration:5.0];
imgView.transform = CGAffineTransformMakeRotation (degrees);
[UIView commitAnimations];

iPhone: Launching iTunes from Apps

Wednesday, October 28th, 2009
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://phobos.apple.com/WebObjects/your_app_link_here"]];

iPhone: cocos2d: Fix flicker at start up

Tuesday, September 15th, 2009

Put this code right before you run you first scene ( Assuming that you have “Default.png” file ):

// prevent flicker
Sprite *sprite = [[Sprite spriteWithFile:@"Default.png"] retain];
sprite.anchorPoint = CGPointZero;
[sprite draw];
[[[Director sharedDirector] openGLView] swapBuffers];
[sprite release];

// Run the intro Scene
[[Director sharedDirector] runWithScene: introScene];

iPhone: Current Time in Milliseconds

Thursday, August 27th, 2009

This is how to get Unix time

[[NSDate date] timeIntervalSince1970];

iPhone: cocos2d: How to change Label text color

Wednesday, July 29th, 2009

Here’s a fun and easy way :)

my_label = [[Label labelWithString:str fontName:@"Helvetica" fontSize:24.0] retain] ;
[ my_label  setRGB:0 :0 :0 ] ;

This post will be very VERY short.

Sunday, July 19th, 2009

Do not enumerate over the mutable array, make a copy of the array instead. That’s it. This will save the pain of debugging.
Cheers.