Wednesday, 9 January 2013

Calendar - Multiple month

///Here is method to add multiple month into scroll view as like iPad Reminder application
//Replace your scroll view name

-(NSDate *)getCurrentDate{
   
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
   
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setCalendar:calendar];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
   
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
   
    return [NSDate dateWithTimeInterval:0 sinceDate:[dateFormatter dateFromString:[dateFormat stringFromDate:[NSDate date]]]];
}
-(void)addCalander
{
    NSDate *yourDate=[self getCurrentDate];
   
    float s=7000;
    float h=0;
    float heightSC=0;
   
    for(int m=0;m<=11;m++)
    {
        TKCalendarMonthView *calendar1=[[TKCalendarMonthView alloc] init];
        calendar1.delegate = self;
        calendar1.dataSource = self;
       
       
        NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *todayComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:yourDate];
        NSInteger theDay = [todayComponents day];
        NSInteger theMonth = [todayComponents month];
        NSInteger theYear = [todayComponents year];
       
        // now build a NSDate object for yourDate using these components
        NSDateComponents *components = [[NSDateComponents alloc] init];
        [components setDay:theDay];
        [components setMonth:theMonth];
        [components setYear:theYear];
        NSDate *thisDate = [gregorian dateFromComponents:components];


        h=calendar1.frame.size.height+2;
       
        // now build a NSDate object for the next day
        NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
        [offsetComponents setMonth:-1];
        NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate:thisDate options:0];
        [calendar1 selectDate:nextDate];
        s=s-calendar1.frame.size.height+2;
               
        yourDate=nextDate;
        heightSC=heightSC+calendar1.frame.size.height+2;
    }
   
   
    yourDate=[self getCurrentDate];
    s=heightSC;
   
    for(int m=11;m>=0;m--)
    {
        TKCalendarMonthView *calendar1=[[TKCalendarMonthView alloc] init];
        calendar1.delegate = self;
        calendar1.dataSource = self;
       
        NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *todayComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:yourDate];
        NSInteger theDay = [todayComponents day];
        NSInteger theMonth = [todayComponents month];
        NSInteger theYear = [todayComponents year];
       
        // now build a NSDate object for yourDate using these components
        NSDateComponents *components = [[NSDateComponents alloc] init];
        [components setDay:theDay];
        [components setMonth:theMonth];
        [components setYear:theYear];
        NSDate *thisDate = [gregorian dateFromComponents:components];
        h=calendar1.frame.size.height;
        // now build a NSDate object for the next day
        NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
        [offsetComponents setMonth:-1];
        NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate:thisDate options:0];
       
        [calendar1 selectDate:nextDate];
        s=s-calendar1.frame.size.height;
        UIView *view_c=[[UIView alloc]initWithFrame:CGRectMake(0, s, 224, calendar1.frame.size.height)];
       
        [self.view addSubview:scroll];
        [view_c addSubview:calendar1];
        [scroll addSubview:view_c];
        yourDate=nextDate;
    }
   
   
    //NSLog(@"s out is %f",s);
    yourDate=[self getCurrentDate];
    s=heightSC;
    for(int m=11;m<35;m++)
    {
        TKCalendarMonthView *calendar1=[[TKCalendarMonthView alloc] init];
        calendar1.delegate = self;
        calendar1.dataSource = self;
       
        NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *todayComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:yourDate];
        NSInteger theDay = [todayComponents day];
        NSInteger theMonth = [todayComponents month];
        NSInteger theYear = [todayComponents year];
        int fla=1;
        if(theDay==1){
            fla=0;
        }
        // now build a NSDate object for yourDate using these components
        NSDateComponents *components = [[NSDateComponents alloc] init];
        [components setDay:theDay];
        [components setMonth:theMonth];
        [components setYear:theYear];
        NSDate *thisDate = [gregorian dateFromComponents:components];
       
        // now build a NSDate object for the next day
        NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
        if(fla==0)
        {
            [offsetComponents setDay:32];
        }
        else
        {
            [offsetComponents setMonth:1];
        }
        NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate:thisDate options:0];
        if(m==11)
        {
            nextDate=yourDate;
            calendarScrollingPosition = s;
        }
        [calendar1 selectDate:nextDate];
       
        UIView *view_c=[[UIView alloc]initWithFrame:CGRectMake(0, s, 224, calendar1.frame.size.height)];
       
       
        s=s+calendar1.frame.size.height+2;
       
        [self.view addSubview:view_c];
        [scroll addSubview:view_c];
        [view_c addSubview:calendar1];
       
        yourDate=nextDate;
    }
   
    scroll.contentSize=CGSizeMake(150, s);
    [scroll setShowsHorizontalScrollIndicator:NO];
    [scroll setShowsVerticalScrollIndicator:NO];
   
    [scroll setContentOffset:CGPointMake(0, calendarScrollingPosition) animated:NO];
}

Monday, 7 January 2013

Audio - Play Sound File

 //Code to play audio from resource
    NSString *path = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath], @"/test.wav"];//Replace with your sound file which is in resource file
   
    SystemSoundID soundID;
    NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
    AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
    AudioServicesPlaySystemSound(soundID);

Database - Select particular row with where condition

//TableName - Name of table
//withColumn - Array of column name to select
//withCondition - column name on which you want to make condition
//ComumnValue - what pass in that column
//Here is method for same
- (NSMutableArray *)columeFromDatabase:(NSString *)TableName withColumn:(NSArray *)arrColumnName withCondition:(NSString *)ColumnNameCondition withColumnValue:(NSString *)ColumnValue
{
    NSMutableArray  *resultArray = [[NSMutableArray alloc] initWithCapacity:0];
    NSString *strComma=@",";
   
    NSMutableString *query=[[[NSMutableString alloc]init] autorelease];
    NSMutableString *strtemp=[[[NSMutableString alloc]init] autorelease];
   
    [query appendString:@"SELECT "];
    for(int i=0;i<[arrColumnName count];i++)
    {
        [query appendString:[arrColumnName objectAtIndex:i]];
        [query appendString:strComma];
       
    }
    NSString *strQueryStar=[NSString stringWithFormat:@"%@",query];
    strQueryStar = [strQueryStar substringToIndex:[strQueryStar length] - 1];
    [strtemp appendString:strQueryStar];
   
    [strtemp appendString:@" FROM "];
    [strtemp appendString:TableName];
    [strtemp appendString:@" WHERE "];
   
    [strtemp appendString:ColumnNameCondition];
    [strtemp appendString:ColumnValue];
   
    const char *sql = [strtemp UTF8String];
    sqlite3_stmt *selectStatement;
   
    //prepare the select statement
    int returnValue = sqlite3_prepare_v2(database, sql, -1, &selectStatement, NULL);
    if(returnValue == SQLITE_OK)
    {
        sqlite3_bind_text(selectStatement, 1, sql, -1, SQLITE_TRANSIENT);
        //loop all the rows returned by the query.
        NSMutableArray *arrColumns = [[NSMutableArray alloc] initWithCapacity:0];
        for (int i=0; i<sqlite3_column_count(selectStatement); i++)
        {
            const char *st = sqlite3_column_name(selectStatement, i);
            [arrColumns addObject:[NSString stringWithCString:st encoding:NSUTF8StringEncoding]];
        }
        int intRow =1;
        while(sqlite3_step(selectStatement) == SQLITE_ROW)
        {
            NSMutableDictionary *dctRow = [[NSMutableDictionary alloc] initWithCapacity:0];
            for (int i=0; i<sqlite3_column_count(selectStatement); i++)                
            {
                int intValue = 0;
                double dblValue =0;
                const char *strValue;
                switch (sqlite3_column_type(selectStatement,i))
                {
                    case SQLITE_INTEGER:
                        intValue  = (int)sqlite3_column_int(selectStatement, i);
                        [dctRow setObject:[NSNumber numberWithInt:intValue] forKey:[arrColumns objectAtIndex:i]];
                        break;
                    case SQLITE_FLOAT:
                        dblValue = (double)sqlite3_column_double(selectStatement, i);
                        [dctRow setObject:[NSNumber numberWithDouble:dblValue] forKey:[arrColumns objectAtIndex:i]];                       
                        break;
                    case SQLITE_TEXT:
                        strValue = (const char *)sqlite3_column_text(selectStatement, i);
                        [dctRow setObject:[NSString stringWithCString:strValue encoding:NSUTF8StringEncoding] forKey:[arrColumns objectAtIndex:i]];                       
                        break;
                    case SQLITE_BLOB:
                        strValue = (const char *)sqlite3_column_value(selectStatement, i);
                        [dctRow setObject:[NSString stringWithCString:strValue encoding:NSUTF8StringEncoding] forKey:[arrColumns objectAtIndex:i]];                       
                        break;
                    case SQLITE_NULL:
                        [dctRow setObject:@"" forKey:[arrColumns objectAtIndex:i]];                       
                        break;
                    default:
                        strValue = (const char *)sqlite3_column_value(selectStatement, i);
                        [dctRow setObject:[NSString stringWithCString:strValue encoding:NSUTF8StringEncoding] forKey:[arrColumns objectAtIndex:i]];
                        break;
                }
               
            }
            [resultArray addObject:dctRow];
            [dctRow release];
            intRow ++;
        }
       
        [arrColumns release];
    }
   
    sqlite3_reset(selectStatement);
    return [resultArray autorelease];
}

UITableView - Reload table view with smooth animation

//Put this code where you want to reload your table view
dispatch_async(dispatch_get_main_queue(), ^{
        [UIView transitionWithView:<"TableName">
                          duration:0.1f
                           options:UIViewAnimationOptionTransitionCrossDissolve
                        animations:^(void) {
                            [<"TableName"> reloadData];
                        } completion:NULL];       
    });

Sunday, 6 January 2013

UIImage - Merge two image

///Mearge two image in one image
-(UIImage *)imageWithImage:(UIImage *)image
                     borderImage:(UIImage *)borderImage
                    covertToSize:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake( 10, 0, 60, 53 )];
   
    [borderImage drawInRect:CGRectMake( 0, 0, 70, 70)];
    UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();   
    UIGraphicsEndImageContext();
    return destImage;
}


///How To Use
  ImageView.image=[self imageWithImage:Image1 borderImage:Image2 covertToSize:CGSizeMake(70, 70)];

MKMap - Add tap gesture on map pin and handle it

Here is code to handle tap gesture on map view and display data related to that when click on pin its directly display its related data.

///Add protocol for UIGestureRecognizerDelegate  

///Add on each pin in viewForAnnotation

 UITapGestureRecognizer *pinTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pinTapped:)];
 pinTap.numberOfTapsRequired=1;
 [yourPinView addGestureRecognizer:pinTap];

///Gesture Handle method
- (void)pinTapped:(UIGestureRecognizer *)gestureRecognizer
{   
    NSLog(@"%i",[gestureRecognizer view].tag);   
    NSLog(@"Write your code");
}

Thursday, 3 January 2013

Keyboard - Add Done Button in Number Pad




//In this code you can add done button on you number key pad you need to handle its hide/show using boolean variable as per your requirement.
.h
==

UIButton * doneButton;
BOOL phoneTagOrNot;

.m
===
- (void)addButtonToKeyboard {
   
    // create custom button
    doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
   
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
        [doneButton setImage:[UIImage imageNamed:@"DoneUp3.png"] forState:UIControlStateNormal];
        [doneButton setImage:[UIImage imageNamed:@"DoneDown3.png"] forState:UIControlStateHighlighted];
       
    } else {
        [doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
        [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
       
    }
   
    [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
   
    // locate keyboard view
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
   
    UIView* keyboard;
   
    for(int i=0; i<[tempWindow.subviews count]; i++) {
       
        keyboard = [tempWindow.subviews objectAtIndex:i];
       
        // keyboard found, add the button
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
            if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
                [keyboard addSubview:doneButton];
           
        } else {
            if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
                [keyboard addSubview:doneButton];
        }

        if (phoneTagOrNot == TRUE)
            doneButton.hidden = TRUE;
        else
            doneButton.hidden = FALSE;
    }
}

Tuesday, 1 January 2013

UITextField - Space in image textbox

@implementation UITextField (custom)
//In this you can set your own space for it
- (CGRect)textRectForBounds:(CGRect)bounds{
    return CGRectMake(bounds.origin.x
                      +20, bounds.origin.y ,
                      bounds.size.width - 40, bounds.size.height);
}
- (CGRect)editingRectForBounds:(CGRect)bounds{
    return [self textRectForBounds:bounds];
}
@end