Thursday, 8 November 2012

How to set Tabbar with ViewController..


set tabbarcontroller with viewcontroller like this in `AppDelegate.m` file in `didFinishLaunchingWithOptions:` method for example..

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
           UIViewController *viewController1 = [[[yourViewController1 alloc] initWithNibName:@"yourViewController1" bundle:nil] autorelease];
            UINavigationController *navviewController1=[[UINavigationController alloc]initWithRootViewController:viewController1];

           
            UIViewController *viewController2 = [[[yourViewController2 alloc] initWithNibName:@"yourViewController2" bundle:nil] autorelease];
            UINavigationController *navviewController2=[[UINavigationController alloc]initWithRootViewController:viewController2];

           
            UIViewController *viewController3 = [[[yourViewController3 alloc] initWithNibName:@"yourViewController3" bundle:nil] autorelease];
            UINavigationController *navviewController3=[[UINavigationController alloc]initWithRootViewController:viewController3];

           
            UIViewController *viewController4 = [[[yourViewController4 alloc] initWithNibName:@"yourViewController4" bundle:nil] autorelease];
            UINavigationController *navviewController4=[[UINavigationController alloc]initWithRootViewController:viewController4];

           
            UIViewController *viewController5 = [[[yourViewController5 alloc] initWithNibName:@"yourViewController5" bundle:nil] autorelease];
            UINavigationController *navviewController5=[[UINavigationController alloc]initWithRootViewController:viewController5];

           
            self.tabBarController = [[[UITabBarController alloc] init] autorelease];
            self.tabBarController.viewControllers = [NSArray arrayWithObjects:navviewController1, navviewController2,navviewController3,navviewController4,navviewController5, nil];
        [self.window makeKeyAndVisible];
        return YES;
    }

Wednesday, 7 November 2012

NSUserDefault with insert,update,delete facility.


In AppDelegate.h file just declare variable...
NSUserDefaults  *userDefaults;
NSMutableArray *yourArray;
after...
In AppDelegate.m Fille in applicationDidFinishLonching: Method
    userDefaults = [NSUserDefaults standardUserDefaults];
    NSData *dataRepresentingtblArrayForSearch = [userDefaults objectForKey:@"yourArray"];    if (dataRepresentingtblArrayForSearch != nil) {
        NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingtblArrayForSearch];
        if (oldSavedArray != nil)
            yourArray = [[NSMutableArray alloc] initWithArray:oldSavedArray];        else
            yourArray = [[NSMutableArray alloc] init];    } else {
        yourArray = [[NSMutableArray alloc] init];    }
    [yourArray retain];
after that when you want to insert,update or delete Data from this UserDefaults Use Bellow Code...
for Delete record from array
[appDelegate. yourArray removeObjectAtIndex:Index];/// give the integer value instead of Index
and this is for add record in array..
[appDelegate. yourArray addObject:AnyValue];///add value or here
after in final save this modification in array just archive like bellow..
NSData *data=[NSKeyedArchiver archivedDataWithRootObject:appDelegate. yourArray];[appDelegate.userDefaults setObject:data forKey:@"yourArray"];[appDelegate.userDefaults synchronize];
i hope this is helpful to you..

Tuesday, 6 November 2012

Time Out Facility in iPhone SDK


Use this code when you want to put requirement of Time Out with any view controller .. here in which view controller you want to put this facility then use this simple code and add in your file..
@interface MainViewController : UIViewController
{
    NSTimer *idleTimer;
}
@end

#define kMaxIdleTimeSeconds 60.0

@implementation MainViewController

#pragma mark -
#pragma mark Handling idle timeout

- (void)resetIdleTimer {
    if (!idleTimer) {
        idleTimer = [[NSTimer scheduledTimerWithTimeInterval:kMaxIdleTimeSeconds
                                                      target:self
                                                    selector:@selector(idleTimerExceeded)
                                                    userInfo:nil
                                                     repeats:NO] retain];
    }
    else {
        if (fabs([idleTimer.fireDate timeIntervalSinceNow]) < kMaxIdleTimeSeconds-1.0) {
            [idleTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:kMaxIdleTimeSeconds]];
        }
    }
}

- (void)idleTimerExceeded {
    [idleTimer release]; idleTimer = nil;
    [self startScreenSaverOrSomethingInteresting];
    [self resetIdleTimer];
}

- (UIResponder *)nextResponder {
    [self resetIdleTimer];
    return [super nextResponder];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self resetIdleTimer];
}

@end
get another example for this facility from this answer of stack-overflow iphone-detecting-user-inactivity-idle-time-since-last-screen-touch 

Tuesday, 16 October 2012

Set toolbar for keyboard


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardDidShow:) 
                                                 name:UIKeyboardDidShowNotification 
                                               object:nil];
    return YES;
}

- (void)keyboardDidShow:(NSNotification *)note
{
    UIButton *returnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    returnBtn.frame = CGRectMake(0,-25,320,25);
    returnBtn.adjustsImageWhenHighlighted = NO;
    returnBtn.backgroundColor=[UIColor darkGrayColor];
    returnBtn.titleLabel.textColor=[UIColor whiteColor];
    [returnBtn setBackgroundImage:[UIImage imageNamed:@"keyBtn.png"] forState:UIControlStateNormal];
    
    [returnBtn addTarget:self action:@selector(keyboardBtn:) 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([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
            //  if (txtTag==5) {
            [keyboard addSubview:returnBtn];
    }
}

-(IBAction)keyboardBtn:(id)sender
{
    [yourTextField resignFirstResponder];/// resign all textfield
}

Local Notification....


- (void) scheduleAlarm:(NSString *)PaymentTitle FireDate:(NSDate *)tempFireDate {
    
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
    
    
    NSDate *pickerDate = tempFireDate; //Set yourDate here
    
    // Break the date up into components
    NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnitNSDayCalendarUnit )
  fromDate:pickerDate];
    NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
  fromDate:pickerDate];
    // Set up the fire time
    NSDateComponents *dateComps = [[NSDateComponents alloc] init];
    [dateComps setDay:[dateComponents day]];
    [dateComps setMonth:[dateComponents month]];
    [dateComps setYear:[dateComponents year]];
    [dateComps setHour:[timeComponents hour]];
// Notification will fire in one minute
    [dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
    NSDate *itemDate = [calendar dateFromComponents:dateComps];
    [dateComps release];
    
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
        return;
    localNotif.fireDate = itemDate;
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
    
// Notification details
    localNotif.alertBody = [NSString stringWithFormat:@"Get %@ Payment of %@ Project",PaymentTitle,strProjectName];
// Set the action button
    localNotif.alertAction = @"View";
    
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 1;
    
// Specify custom data for the notification
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"iClientManagement" forKey:@"App"];
    localNotif.userInfo = infoDict;
    
// Schedule the notification
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    [localNotif release];
}

Tuesday, 9 October 2012

Sort the NSMutableArray with date with string type


 NSMutableArray *tempDateArray = [[NSMutableArray alloc]init];
        for (int i = 0; i < [arrProjectList count]; i++) {
            NSString *strThisDate = [[arrProjectList objectAtIndex:i]valueForKey:@"Last_Date"];
            NSLog(@"\n\n Date here %@",strThisDate);
            [strThisDate retain];
            
            NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
            [dateFormatter setDateFormat:@"EEE, dd-LLL-yyyy"];
            
            NSDate *date = [dateFormatter dateFromString: strThisDate];
            //        date = [self convertStringToDate:strThisDate];
            strThisDate = [date stringFromDateWithFormat:@"yyyy-MM-dd 00:00:00 +0000"];
            [tempDateArray addObject:strThisDate];
        }
        [tempDateArray retain];
        NSArray *arrDates = [[NSArray alloc]initWithArray:tempDateArray];
        [arrDates retain];
        [tempDateArray sortUsingSelector:@selector(compare:)];

        NSMutableArray *temparrProjectList = [[NSMutableArray alloc]init];
        for (int j = 0; j < [tempDateArray count]; j++) {
            NSString *tempstrThisDate = [tempDateArray objectAtIndex:j];
            for (int i = 0; i < [arrProjectList count]; i++) {
                NSString *strThisDate = [[arrProjectList objectAtIndex:i]valueForKey:@"Last_Date"];
                NSLog(@"\n\n Date here %@",strThisDate);
                [strThisDate retain];
                
                NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
                [dateFormatter setDateFormat:@"EEE, dd-LLL-yyyy"];
                
                NSDate *date = [dateFormatter dateFromString: strThisDate];
                //        date = [self convertStringToDate:strThisDate];
                strThisDate = [date stringFromDateWithFormat:@"yyyy-MM-dd 00:00:00 +0000"];
                if ([strThisDate isEqualToString:tempstrThisDate]) {
                    if (![temparrProjectList containsObject:[arrProjectList objectAtIndex:i]]) {
                        [temparrProjectList addObject:[arrProjectList objectAtIndex:i]];    
                    }
                }
                
            }
        }
        [temparrProjectList retain];
        arrProjectList = temparrProjectList;

Local Notification in iPhone SDK.


Change some code and get your output...
here i give the logic for set notification before 1 day of selected day.

-(IBAction)btnGo_Clicked:(id)sender{

    NSLog(@"\n ----------->>Go Clicked");
    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls != nil) {
        NSString *kRemindMeNotificationDataKey = @"kRemindMeNotificationDataKey";

        ///bellow i set 1 day before give us notification
        NSDateComponents *dc = [[NSCalendar currentCalendar] components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSQuarterCalendarUnit fromDate:[exdatePicker date]];
        [dc setDay:dc.day - 1];
        NSDate *noticeDate = [[NSCalendar currentCalendar] dateFromComponents:dc];

        
        NSDateFormatter* dateFormatterstring = [[NSDateFormatter alloc] init];
        [dateFormatterstring setDateFormat:@"dd-MM-yyyy hh:mm:ss a"];
        NSString *dateString = [dateFormatterstring stringFromDate:noticeDate];

        txtExpirayDate.text = dateString;
        UILocalNotification *notification = [[cls alloc] init];

        notification.fireDate = noticeDate;
        notification.timeZone = [NSTimeZone defaultTimeZone];
        notification.alertBody = [NSString stringWithFormat:@"%@ your Licence Expiry Date is Tommorow",txtLicence.text];
        notification.alertAction = @"Show me";
        notification.soundName = UILocalNotificationDefaultSoundName;
        notification.applicationIconBadgeNumber = 1;
        NSDictionary *userDict = [NSDictionary dictionaryWithObject:txtLicence.text forKey:kRemindMeNotificationDataKey];
        notification.userInfo = userDict;
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
        [notification release];
    }
    exdatePicker.hidden=YES;
}