Monday 22 June 2015

Animate a Table View as Dropdown in iOS

m File:
-(void)viewDidLoad
{
CGRect rect = CGRectMake(lbl_txt.frame.origin.x, lbl_txt.frame.origin.y+40, lbl_txt.frame.size.width, 0);
    [tbl_Cities setFrame:rect];
}


-(IBAction)clicked_cities:(id)sender
{
CGRect rect = CGRectMake(lbl_Selected_City.frame.origin.x, lbl_Selected_City.frame.origin.y+40, lbl_Selected_City.frame.size.width, 250);
    [UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[tbl_Cities setFrame:rect];
[UIView commitAnimations];

}

Animate a frames, After Load a Function in iOS

.m File:
-(IBAction)clicked_Language:(id)sender
{
    [UIView animateWithDuration:1.2f delay:0.0f options:UIViewAnimationOptionTransitionFlipFromRight animations:^
     {
         logo.frame = CGRectMake(180, 80, 215, 170);
         btn.frame = CGRectMake(180, 259, 228, 55);
         
     } completion:^(BOOL isDone)
     {
         if (isDone)
         {
             [self load_languages];
         }
     }];
}

-(void)load_languages
{
    //view_logo.frame = CGRectMake(53, 80, 215, 170);
    
    btn.alpha = 1;
    [UIView beginAnimations:@"animateTableView" context:nil];
    [UIView setAnimationDuration:0.5];
    btn.alpha = 0;
    [UIView commitAnimations];
    btn.hidden = YES;
    
    view_table.hidden = NO;
    view_table.alpha = 0;
    view_table.frame = CGRectMake(btn.frame.origin.x, btn.frame.origin.y+5, 300, 300);
    [UIView beginAnimations:@"animateTableView" context:nil];
    [UIView setAnimationDuration:1.2];
    view_table.alpha = 1;
    [UIView commitAnimations];
    
    [table_language reloadData];
}

Dynamically Assign Selected Value Placed in TextField using Tag for iOS

DetailViewController.h
UIView *detail_view;
NSArray *functions;
int tag_selected;
UITextField *text_field;
UITableView *btn_dropdown;
UITableView *tbl_dropdown;


DetailViewController.m
-(void)viewDidLoad
{
detail_view = [[UIView alloc]init];

functions = [[NSArray alloc]initWithObjects:@"Buy Gold",@"Buy Silver",@"Buy Diamond",@"Golden day",@"Good Friday",@"Pongal", nil];
[tbl_dropdown reloadData];

int y_val = 60;
    for (int q = 0; q < functions.count; q++)
    {
        text_field = [[UITextField alloc]init];
        text_field.tag = q;
        text_field.frame = CGRectMake(13, y_val, 230, 40);
        UIView *paddingTextfield = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 42)];
        text_field.leftView = paddingTextfield;
        text_field.leftViewMode = UITextFieldViewModeAlways;
        text_field.placeholder = @"Enter Text";
        text_field.font = [UIFont fontWithName:@"Arial" size:18.0];
        text_field.textColor = [UIColor brownColor];
        text_field.borderStyle = UITextBorderStyleNone;
        text_field.returnKeyType = UIReturnKeyDone;
        text_field.delegate = self;
        text_field.backgroundColor = [UIColor whiteColor];
        text_field.layer.cornerRadius = 3;
        [detail_view addSubview:text_field];

btn_dropdown = [UIButton buttonWithType:UIButtonTypeCustom];
    btn_dropdown.tag = q;
    btn_dropdown.frame = text_field.frame;
    UIImage *btnImage= [UIImage imageNamed:@“cust_btn.png"];
    [btn_dropdown setImage:btnImage forState:UIControlStateNormal];
    [detail_view addSubview:btn_dropdown];
        
        y_val = y_val + 20;
    }
}

-(void)drop_table_clicked:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    tag_selected = btn.tag;
tbl_dropdown.hidden = NO;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == tbl_dropdown)
    {
        return [functions count];
    }
return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cell_Identifier = @"cell_Identifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_Identifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell_Identifier];
    }
    
    if (tableView == tbl_dropdown)
    {
        cell.textLabel.text = [functions objectAtIndex:indexPath.row];
}
return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == tbl_dropdown)
    {
        tbl_dropdown.hidden = YES;
        NSString *str_text = [functions objectAtIndex:indexPath.row];
        NSLog(@"%@", str_text);
        
        NSLog(@"%d", tag_selected);
        text_field = (UITextField *)[detail_view viewWithTag:tag_selected];
        text_field.text = str_text;
    }
}

Find the System Name & Screen Size

Find the Screen Size
CGSize result = [[UIScreen mainScreenbounds].size;

Find the System Name
#import <sys/utsname.h>
-(void)findversiondetails
{
    //Find device type
    struct utsname systemInfo;
     uname(&systemInfo);
     
     NSString *deviceModel = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
     
     if ([deviceModel isEqualToString:@"iPhone4,1"] || [deviceModel isEqualToString:@"iPhone3,3"] || [deviceModel isEqualToString:@"iPhone3,2"] || [deviceModel isEqualToString:@"iPhone3,1"])
     {
         UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Title" message:deviceModel delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
         [alert show];
     }
}

Local Notification in iOS


AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    application.applicationIconBadgeNumber = 0;
    
    return YES;
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    UIApplicationState state = [application applicationState];
    
    if (state == UIApplicationStateActive)
    {
        NSDate *date = [NSDate date];
        NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
        timeFormatter.dateFormat = @"dd.MMM.yyyy";
        NSString *EventTitle = [NSString stringWithFormat:@"%@ - Today Events",[timeFormatter stringFromDate:date]];
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:EventTitle
                                                        message:notification.alertBody
                                                       delegate:self cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }
    
    // Request to reload table view data
    [[NSNotificationCenter defaultCenter] postNotificationName:@"addEvent" object:self];
    
    // Set icon badge number to zero
    application.applicationIconBadgeNumber = 0;
}


LocalNotifyController.m
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addEvent) name:@"addEvent" object:nil];
    
    [self reloaddata];
    [self remaining_days];
}


-(void)reloaddata
{
    //add Events
    NSArray *eventdates = [[NSArray alloc]initWithObjects:@"2015-01-09",@"2015-01-10",@"2015-01-11",@"2015-01-12",@"2015-01-13",@"2015-01-14", nil];
    NSArray *eventfunctions = [[NSArray alloc]initWithObjects:@"Buy Gold",@"Buy Silver",@"Buy Diamond",@"Golden day",@"Good Friday",@"Pongal", nil];
    
    for (int r=0; r<5; r++)
    {
        // Schedule the notification
        NSString *event = [NSString stringWithFormat:@"%@ 17:00:00",[eventdates objectAtIndex:r]];
        
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        [df setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
        NSDate *myDate = [df dateFromString:event];
        
        UILocalNotification* localNotification = [[UILocalNotification alloc] init];
        localNotification.fireDate = myDate;//@"2015-01-09 11:03:31 +0000";
        localNotification.alertBody = [eventfunctions objectAtIndex:r];
        localNotification.alertAction = @"Continue";
        localNotification.soundName = UILocalNotificationDefaultSoundName;
        localNotification.timeZone = [NSTimeZone localTimeZone];
        localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
        
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
        
        // Request to reload table view data
        [[NSNotificationCenter defaultCenter] postNotificationName:@"addEvent" object:self];
    }
}

-(void)addEvent
{
    //add more event
    
    //NSUserDefaults to find last date & add more events in UILocalNotification.
}

-(void)remaining_days
{
    //Get Count of Remaining Notifications
    int k = (int)[[[UIApplication sharedApplication] scheduledLocalNotifications] count];
    NSLog(@"%d", k);
    
    // Get list of local notifications
    NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
    
    for (int i = 0; i < localNotifications.count; i++)
    {
        UILocalNotification *localNotification = [localNotifications objectAtIndex:i];
        
        // Display notification info
        NSString *str_Notify_Name = localNotification.alertBody;
        NSString *str_Notify_Date = [localNotification.fireDate description];
        
        NSLog(@"%@, %@", str_Notify_Name, str_Notify_Date);
    }

}

Add a Calendar into iOS - CKCalenderView

Create a ViewController File for Calendar. In Framework File use to customise the functions & Values in iOS [You can set the Minimum date, Enable dates, Set Images & More].

Add the Frameworks: UIKit, Foundation, CoreGraphics, QuartzCore

Download or copy the Two Files. Added code into your project.
CKCalenderView.h 
CKCalenderView.m 

Download Sample Code
(or)


In Framework File - Add the Code for CKCalenderView.h : #import <UIKit/UIKit.h>

CalenderViewController.h
#import <UIKit/UIKit.h>

@interface CalenderViewController : UIViewController
{
    IBOutlet UITableView *table_view;
}

@end


CalenderViewController.m
#import <CoreGraphics/CoreGraphics.h>
#import "CKCalendarView.h"

@interface CalenderViewController ()<CKCalendarDelegate>
{
    NSMutableArray *Dates;
}

@property(nonatomicweakCKCalendarView *calendar;
@property(nonatomicstrongUILabel *dateLabel;
@property(nonatomicstrongNSDateFormatter *dateFormatter;
@property(nonatomicstrongNSDate *minimumDate;
@property(nonatomicstrongNSMutableArray *disabledDates;

@end

@implementation CalenderViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @“Calendar Days";
    
    CKCalendarView *calendar = [[CKCalendarView allocinitWithStartDay:startSunday];
    self.calendar = calendar;
    calendar.delegate = self;
    //self.calendar.backgroundColor = [UIColor blueColor];
    
    self.dateFormatter = [[NSDateFormatter allocinit];
    [self.dateFormatter setDateFormat:@"dd/MM/yyyy"];
    self.minimumDate = [self.dateFormatter dateFromString:@"20/09/2014"];
    
    self.disabledDates = [[NSMutableArray alloc]init];
    
    Dates = [[NSMutableArray alloc]initWithObjects:@"22/12/2014"@"25/12/2014"@"29/12/2014",  @"01/01/2015"@"21/01/2015"@"15/04/2015"nil];
    
    for (int i = 0; i<[Dates count]; i++)
    {
        NSString *str_date = [Dates objectAtIndex:i];
        [self.disabledDates addObject:[self.dateFormatter dateFromString:str_date]];
    }
    
    calendar.onlyShowCurrentMonth = YES;
    calendar.adaptHeightToNumberOfWeeksInMonth = NO;
    
    calendar.frame = CGRectMake(1080300320);
    [self.view addSubview:calendar];
    
    self.dateLabel = [[UILabel allocinitWithFrame:CGRectMake(10CGRectGetMaxY(calendar.frame) + 4self.view.bounds.size.width24)];
    [self.view addSubview:self.dateLabel];
    
    self.view.backgroundColor = [UIColor whiteColor];
}


- (void)dealloc
{
    [[NSNotificationCenter defaultCenterremoveObserver:self];
}


- (void)localeDidChange
{
    [self.calendar setLocale:[NSLocale currentLocale]];
}


- (BOOL)dateIsDisabled:(NSDate *)date
{
    for (NSDate *disabledDate in self.disabledDates)
    {
        if ([disabledDate isEqualToDate:date])
        {
            return YES;
        }
    }
    return NO;
}


#pragma mark -
#pragma mark - CKCalendarDelegate

- (void)calendar:(CKCalendarView *)calendar configureDateItem:(CKDateItem *)dateItem forDate:(NSDate *)date
{
    // TODO: play with the coloring if we want to...
    if ([self dateIsDisabled:date])
    {
        dateItem.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"red.png"]];
        dateItem.textColor = [UIColor whiteColor];
    }
}


- (BOOL)calendar:(CKCalendarView *)calendar willSelectDate:(NSDate *)date
{
    return ![self dateIsDisabled:date];
}


- (void)calendar:(CKCalendarView *)calendar didSelectDate:(NSDate *)date
{
    self.dateLabel.text = [self.dateFormatter stringFromDate:date];
}


- (BOOL)calendar:(CKCalendarView *)calendar willChangeToMonth:(NSDate *)date
{
    if ([date laterDate:self.minimumDate] == date)
    {
        return YES;
    }
    else
    {
        return NO;
    }
}


- (void)calendar:(CKCalendarView *)calendar didLayoutInRect:(CGRect)frame
{
    NSLog(@"calendar layout: %@"NSStringFromCGRect(frame));
}


#pragma mark -
#pragma mark - UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [Dates count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell allocinitWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    
    cell.textLabel.text = [Dates objectAtIndex:indexPath.row];
    return cell;
}

@end

Custom Calendar for iOS

Custom Calendar for iOS
Releasing Soon

Zoom Out the MapView for iOS

Add the Frameworks: MapKit, CoreLocation

Create a New File for MapViewController & Add the Below Code’s

MapViewController.h
#import <MapKit/MapKit.h>

@interface MapViewController : UIViewController <MKMapViewDelegate>

@property (weaknonatomicIBOutlet MKMapView *map_view;

@end


MapViewController.m
#import "MapViewController.h"

@interface MapViewController ()
@end

@implementation MapViewController

@synthesize map_view;

- (void)viewDidLoad
{
    [super viewDidLoad];
    map_view.delegate = self;
map_view.mapType = MKMapTypeStandard;

    [self zoom_out_view];
}

- (void)zoom_out_view
{
// step.1 create co-ordianate-span as follows
    MKCoordinateSpan span = {.latitudeDelta = 180, .longitudeDelta = 360};
    
    // step.2 crate region as follows
    MKCoordinateRegion region = MKCoordinateRegionMake(CLLocationCoordinate2DMake(0.0000f0.0000f), span);
    
    // step.3 zoom using region to map as follows
    [map_view setRegion:region animated:YES];

}

Drawing Route - MKMapView for iOS

(or) 

Add the Frameworks : MapKit, CoreLocation

Add the Files : Place.h, Place.m, PlaceMark.h, PlaceMark.m, MapView.h, MapView.m, RegexKitLite.h, RegexKitLite.m


Add the linker flag in Project Settings : "Build Settings" -> "Linking" -> "Other Linker Flags" add the flag as "-licucore"

Set the Non ARC Flag for Files : Place.m, PlaceMark.m, MapView.m, RegexKitLite.m

Add the Lines in Info.Plist File : NSLocationAlwaysUsageDescription, NSLocationWhenInUseUsageDescription



MapViewController.h
#import <UIKit/UIKit.h>
#import "MapView.h"
#import "Place.h"

@interface MapViewController : UIViewController 
{

}

@end


MapViewController.m
- (void)viewDidLoad 
{
    [super viewDidLoad];

MapView* mapView = [[MapView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    
    [self.view addSubview:mapView];
    
    Place* home = [[Place alloc] init];
    home.name = @"Home";
    home.description = @"Sweet home";
    home.latitude = 41.029598;
    home.longitude = 28.972985;
    
    Place* office = [[Place alloc] init];
    office.name = @"Office";
    office.description = @"Good office";
    office.latitude = 41.033586;
    office.longitude = 28.984546;
    
    [mapView showRouteFrom:home to:office];
}



-(void)getCurrentLocation
{
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    if(IS_OS_8_OR_LATER)
    {
        NSUInteger code = [CLLocationManager authorizationStatus];
        if (code == kCLAuthorizationStatusNotDetermined && ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)] || [locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]))
        {
            // choose one request according to your business.
            if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"])
            {
                [locationManager requestAlwaysAuthorization];
            }
            else if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"])
            {
                [locationManager requestWhenInUseAuthorization];
            }
            else
            {
                NSLog(@"Info.plist does not contain NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription");
            }
        }
    }
    [locationManager startUpdatingLocation];
}


#pragma mark - CLLocationManagerDelegate
#pragma - didFailWithError
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
    
    
}

#pragma - didUpdateLocation
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    [locationManager stopUpdatingLocation];   // when u got the lat and long it stop update the location
    
    CLLocation *currentLocation = newLocation;
    
    if (currentLocation != nil)
    {
        getcurrlong = currentLocation.coordinate.longitude;
        getcurrlat = currentLocation.coordinate.latitude;
        
        NSLog(@"%f", getcurrlat);
        NSLog(@"%f", getcurrlong);
        
        [self Route];

// Reverse Geocoding - Resolving Address
    NSLog(@"Resolving the Address");
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
        if (error == nil && [placemarks count] > 0) {
            CLPlacemark *placemark = [placemarks lastObject];
            NSLog(@"%@",[NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@", placemark.subThoroughfare, placemark.thoroughfare, placemark.postalCode, placemark.locality, placemark.administrativeArea,placemark.country]);
        } else {
            NSLog(@"%@", error.debugDescription);
        }
    }];
    }

}