Tuesday, 4 June 2013

How to create iOS application badge: cocos2d/Xcode

In many iOS apps, you may have noticed the number indicator inside a red circle above the application icon. It is called icon badge or simply badge.




The badge is used mainly for indicating local or push notification messages. You can create the icon badge with desired number inside with a very easy, single line code as below:

Syntax:

        [UIApplication sharedApplication].applicationIconBadgeNumber = 23;

How to convert the normal textfield keyboard to e-mail keyboard:cocos2d/Xcode

 Mail keyboard or changing keyboardtype:

    mailField.keyboardType = UIKeyboardTypeEmailAddress; // where mailField is the text field

Monday, 3 June 2013

How to programmatically change the device orientation instantly: cocos2d

To change Device orientation instantly, use the following syntax:

Syntax:

    CCDirector *director = [CCDirector sharedDirector];
    [director setDeviceOrientation:kCCDeviceOrientationPortrait];  // set your desired orientation here

How to implement e-mail view - Landscape: cocos2d


// in .h File //

    #import <MessageUI/MFMailComposeViewController.h>
       
    @interface Game : CCLayer <MFMailComposeViewControllerDelegate>
    {
        NSString *emailTitle;
        NSString *emailBody;
        UIImage *emailImage;
        MFMailComposeViewController *picker;
    }
    -(void)showMailPicker;
    -(id)initWithTitle:(NSString *)title body:(NSString *)body image:(UIImage *)image;
     
    // in .m File //

       
    -(id)initWithTitle:(NSString *)title body:(NSString *)body image:(UIImage *)image
    {
        self = [super init];
        if (self != nil) {
            emailTitle = title;
            emailBody = body;
            emailImage = image;
            [self showMailPicker];
        }
        return self;
    }
       
    -(void)showMailPicker
    {
        picker = [[MFMailComposeViewController alloc] init];
        picker.mailComposeDelegate = self;
        picker.modalPresentationStyle = UIModalPresentationFullScreen;
        picker.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
           
        [picker setSubject:emailTitle];
        [picker setMessageBody:emailBody isHTML:YES];
        picker.navigationBar.barStyle = UIBarStyleBlack;
           
        [[CCDirector sharedDirector] pause];
        UIViewController *rootViewController = (UIViewController *)[[[CCDirector sharedDirector] openGLView ] nextResponder];  // This will do it
           
        [rootViewController presentModalViewController:picker animated:YES];
        [picker release];
    }
       
    - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
    {
        [[CCDirector sharedDirector] resume];
        [controller dismissModalViewControllerAnimated: YES];
    }
       

How to implement e-mail view - Portrait: cocos2d

// in .h File //

    #import <MessageUI/MessageUI.h>
   
    @interface EmailScene : CCLayer <MFMailComposeViewControllerDelegate>
    {
        UIViewController * emailMe;
    }



// in .m File //

    #import "EmailScene.h"

    -(id) init
    {
        if( (self=[super init])) {
               
        CCMenuItemImage *Button_1 = [CCMenuItemImage
                                    itemFromNormalImage:@"Icon-72.png"
                                    selectedImage:@"i.png"
                                    target:self
                                    selector:@selector(emailCallback)];
               
        Button_1.position = ccp(160, 240);
               
        CCMenu *levelMenu = [CCMenu menuWithItems:Button_1, nil];
        levelMenu.position = ccp(0,0);
        [self addChild:levelMenu z:1];
               
               
        emailMe = [[UIViewController alloc] init];
        [[[CCDirector sharedDirector] openGLView] addSubview:emailMe.view];
               
        }
        return self;
    }
       
       
    -(void)emailCallback
    {
        [[CCDirector sharedDirector] pause];
        [[CCDirector sharedDirector] stopAnimation];
           
        MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
        picker.mailComposeDelegate = self;
       
        NSArray *toRecipient = [NSArray arrayWithObject:@"krishnaraj@schogini.com"];
        [picker setToRecipients:toRecipient];
       
        [picker setSubject:@"Email test "];
        [picker setMessageBody:@"finally its working " isHTML:YES];
        picker.navigationBar.barStyle = UIBarStyleBlack;
           
        [emailMe presentModalViewController:picker animated:YES];
        [picker release];
    }
       
    - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
    {
        [[CCDirector sharedDirector] resume];
        [[CCDirector sharedDirector] startAnimation];
           
        [controller dismissModalViewControllerAnimated:NO];
    }
       


This will do it.................... ;)

How to create/display UILabel programmatically:cocos2d

To create an UILabel in the scene, you can use the below code:

    // in .h File //

    UIView *profileView;
    UILabel *labelName;

    // in .m File //

    -(void)show_{

        // Create a UIView //
        profileView = [[UIView alloc] init];
        profileView.frame = CGRectMake(0, 0, 320, 480);
        profileView.backgroundColor = [UIColor blueColor];
        [[[CCDirector sharedDirector] openGLView] addSubview:profileView];

        // Create the UIlabel and add it to the UIView //
        labelName = [[UILabel alloc] init];
        labelName setText:[NSString stringWithFormat:@"%d",0]];
        labelName.frame = CGRectMake(0, 0,  35, 35);
        labelName.center = CGPointMake(160, 160);
        labelName.backgroundColor = [UIColor clearColor]; 
        labelName.textAlignment = NSTextAlignmentCenter;  
        [profileView addSubview:labelName];     
       
        //----------Line break for UIlabel(optional)----------//
        labelName.lineBreakMode = NSLineBreakByWordWrapping;
        labelName.numberOfLines = 0;
        //---------------------------------------------------//
    }

Get current date, month and year: cocos2d/Xcode

To get current date, month and year, you can use the following code:

    NSDate *now = [NSDate date];

    NSLog(@"now: %@", now); // now: 2011-02-28 09:57:49 +0000

    NSString *strDate = [[NSString alloc] initWithFormat:@"%@",now];
    NSArray *arr = [strDate componentsSeparatedByString:@" "];
    NSString *str;
    str = [arr objectAtIndex:0];
    NSLog(@"strdate: %@",str); // strdate: 2011-02-28

    NSArray *arr_my = [str componentsSeparatedByString:@"-"];

    NSInteger date = [[arr_my objectAtIndex:2] intValue];
    NSInteger month = [[arr_my objectAtIndex:1] intValue];
    NSInteger year = [[arr_my objectAtIndex:0] intValue];

    NSLog(@"year = %d", year); // year = 2011
    NSLog(@"month = %d", month); // month = 2
    NSLog(@"date = %d", date); // date = 2