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;
}

Tuesday, 28 August 2012

For remove UIDevice orientation warning just put bellow code in your file where you want to put fix orientation


For example if you want to your project only support Landscape or Portrait Orientation then when you put bellow line... i.e

-(void)viewWillAppear:(BOOL)animated
{
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
}

then its give warning like...
'UIDevice' may not respond to 'setOrientation:'

so for solve this warning just put bellow code where you use this line


@interface UIDevice (MyPrivateNameThatAppleWouldNeverUseGoesHere)
- (void) setOrientation:(UIInterfaceOrientation)orientation;
@end

Monday, 27 August 2012

Get Address From Latitude,Longitude....

Hi Friends,
Very simple method for get address from latitude and longitude method is bellow..


-(NSString *)getAddressFromLatLon:(double)pdblLatitude withLongitude:(double)pdblLongitude
{
    NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%f,%f&output=csv",pdblLatitude, pdblLongitude];
    NSError* error;
    NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
    locationString = [locationString stringByReplacingOccurrencesOfString:@"\"" withString:@""];
    return [locationString substringFromIndex:6];
}

and for get output just call this method like bellow..

NSString *strAddress = [self getAddressFromLatLon:yourLatitude withLongitude:yourLongitude];

NSLog(@"Address is %@",strAddress);