Sunday, December 16, 2012

How to find a KEY from NSDictionary?

Here we will take NSMutableArray with objects and NSDictionary with Objects and Keys.



    NSMutableArray* mArray = [[NSMutableArray alloc]
                              initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F", nil];
    
    NSMutableDictionary *mDic = [NSMutableDictionary
                                 dictionaryWithObjectsAndKeys:@"F",@"mF",@"G",@"mG",@"B",@"mB",@"I",@"mI",nil];



We will write a Function which can provide us the array of founded keys after comparison.


-(NSMutableArray*)compareMyObject:(NSMutableArray*)array:(NSDictionary*)dic
{
    NSMutableArray* keysArray = [[NSMutableArray alloc] init];
for(int k=0;k<[array count];k++)
{
NSString* mStr = [array objectAtIndex:k];
        NSArray* array = [dic allKeysForObject:mStr];
if([array count])
        {
            [keysArray addObject:[array objectAtIndex:0]];
        }
}
    return keysArray;
}

Now we will call this method in the following format.

    NSMutableArray* keysArray =  [self compareMyObject:mArray:mDic];

The above method gives us the result array now, We will print and check those results.

    NSLog(@"total %d keys found. Those are %@",[keysArray count],[keysArray description]);


The result is : 

keysArray = (
    mB,
    mF




Wednesday, February 1, 2012

How to capture the screen with frame specific?

    CGRect mRect = CGRectMake(0,0,730,905);
    UIGraphicsBeginImageContext(mRect.size);
    [self.containerView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

mRect is any frame which you wanted to take screen shot and containerview is the view where we need to take the screenshot.

A small examples with Animations

In iOS Sdk there are two types of animation which is used frequently.

with CoreAnimation

            CATransition *animation = [CATransition animation];
            [animation setDuration:1.5];
            [animation setType:kCATransitionMoveIn];
            [animation setSubtype:kCATransitionFromRight];
            [animation setSpeed:1.5];
            [[imageView layer] addAnimation:animation forKey:nil];

Here imageView is the view where you need to apply the animation. the view with come from right to left with the specific duration.

setDuration - to set the animation happen time
setType - to set the animation type

see CATransition class to get more details.

with UIAnimation

        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:2.0];
        [UIView setAnimationDelay:1.0];
        [UIView setAnimationDelegate:self];
        myImageView.alpha = 0.0;
        [UIView commitAnimations];

 Here i wanted to hide the myImageview. Instead of hiding with
    myImageView.hidden = YES;
I thought of adding some animation to hide the view.
It will hide the view in 2 sec after 1 sec of delay.


We have delegate methods to use more effectively. Just have a look at the Class.






How to print the Frame(CGRect) of a View?

    NSLog(@"self.view.Frame=%@", NSStringFromCGRect(self.view.frame));

Here self.view is the view of UIView Class. You can print any frame with this log.

Monday, April 18, 2011

jSon Parse

First Import the ASIHttpRequest Classes into our project.

        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:location_cookie_url];
        [request setDelegate:self];
        request.shouldRedirect = NO;
        [request startAsynchronous];   

- (void)requestFinished:(ASIHTTPRequest*)request
{
        if ([[[request responseHeaders] objectForKey:@"Set-Cookie"] hasPrefix:@"Preferences"])
        {

            ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:current_client_url];
            NSString*Strr = [NSString stringWithFormat:@"{\"IsMobile\": true, \"offerGuid\": \"%@\"}",[self.mProduct objectForKey:@"Guid"]];
           
            NSLog(@"%@",Strr);
           
            [request appendPostData:[Strr dataUsingEncoding:NSUTF8StringEncoding]];
            [request setDelegate:self];
            [request addRequestHeader:@"Content-Type" value:@"application/json; charset=utf-8"];
            [request startAsynchronous];
        }
 }

- (void)requestFailed:(ASIHTTPRequest*)request
{
}

Thursday, January 20, 2011

How to reduce the distance between sections in UITableview

Use all these methods to reduce the height between sections in tableview

-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
      return 3.0;
}

-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{
    return 3.0;
}

-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)] autorelease];
}

-(UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
    return [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)] autorelease];
}



Application should run on background or not

  1. Open your info.plist file
  2. Add The Key UIApplicationExitsOnSuspend or Select Application does not run in background
  3. Set the new key to YES or Fill in the tick box

Wednesday, January 19, 2011

How to disable the sleep mode

    [[UIApplication sharedApplication] setIdleTimerDisabled:YES];

Monday, January 17, 2011

Digital Font for the Label

[myLabel setFont:[UIFont fontWithName:@"DBLCDTempBlack" size:42.0]];

Friday, January 14, 2011

Generate Random Number in a given range

TestViewController.h

#define RANDOM_SEED() srandom((unsigned)(mach_absolute_time() & 0xFFFFFFFF))
#define RANDOM_NUM() (random())


@interface TestViewController : UIViewController
...

TestViewController.m



#include <mach/mach_time.h>
@implementation TestViewController
-(int)getRandomNumber
{
RANDOM_SEED();
int randomSelect=RANDOM_NUM()%([yourArray count]-1); // picks a random number in range 0 to count
return randomSelect;
}