Friday 24 January 2014

Splash Screen Animation

AppDelegate.h

#import <UIKit/UIKit.h>

@class ViewController;



@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    UIImageView *splashView;
}

@property (strongnonatomicUIWindow *window;

@property (strongnonatomicViewController *viewController;

- (void)startupAnimationDone:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context;

@end

AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow allocinitWithFrame:[[UIScreen mainScreenbounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController allocinitWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    
    // Make this interesting.
    
    splashView = [[UIImageView allocinitWithFrame:CGRectMake(0,0320480)];
    splashView.image = [UIImage imageNamed:@"Default.png"];
    [self.window addSubview:splashView];
   [self.window bringSubviewToFront:splashView];
    [UIView beginAnimations:@"yourAnim" context:nil];
    [UIView setAnimationDuration:2.0];
   [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.window cache:YES];
   [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(startupAnimationDone:finished:context:)];
    splashView.alpha = 0.0;
    splashView.frame = CGRectMake(-60, -85440635);
    //[UIView commitAnimations];
    
    return YES;
}

- (void)startupAnimationDone:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    [splashView removeFromSuperview];
}

Type 2:



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow allocinitWithFrame:[[UIScreen mainScreenbounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController allocinitWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    
    // Make this interesting.
  
   splashView = [[UIImageView allocinitWithImage:[UIImage imageNamed:@"Default.png"]];
    [splashView sizeToFit];
    
    UIViewController* tmpVC = [UIViewController new];
    [tmpVC.view setFrame:splashView.bounds];
    [tmpVC.view addSubview:splashView];
    
    // just by instantiating a UIWindow, it is automatically added to the app.
    UIWindow *keyWin = [UIApplication sharedApplication].keyWindow;
    UIWindow *hudWindow = [[UIWindow allocinitWithFrame:CGRectMake(0.0f, -20.0f, keyWin.frame.size.width, keyWin.frame.size.height)];
    
    [hudWindow setBackgroundColor:[UIColor clearColor]];
    [hudWindow setRootViewController:tmpVC];
    [hudWindow setAlpha1.0];
    [hudWindow setWindowLevel:UIWindowLevelStatusBar+1];
    [hudWindow setHidden:NO];
    
    UIWindow *_hudWin = hudWindow;
    
    [UIView animateWithDuration:2.0f animations:^{
        [_hudWin setAlpha:0.f];
    } completion:^(BOOL finished) {
        [_hudWin removeFromSuperview];
        
    }];
    
    return YES;
}

POST METHOD PROCESS IN IOS

1. Set post string with actual username and password.
NSString *post = [NSString stringWithFormat:@"&Username=%@&Password=%@",@"username",@"password"];
2. Encode the post string using NSASCIIStringEncoding and also the post string you need to send in NSData format.
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
You need to send the actual length of your data. Calculate the length of the post string.
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
3. Create a Urlrequest with all the properties like HTTP method, http header field with length of the post string. Create URLRequest object and initialize it.
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
Set the Url for which your going to send the data to that request.
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.abcde.com/xyz/login.aspx"]]];
Now, set HTTP method (POST or GET). Write this lines as it is in your code.
[request setHTTPMethod:@"POST"];
Set HTTP header field with length of the post data.
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
Also set the Encoded value for HTTP header Field.
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
Set the HTTPBody of the urlrequest with postData.
[request setHTTPBody:postData];
4. Now, create URLConnection object. Initialize it with the URLRequest.
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
It returns the initialized url connection and begins to load the data for the url request. You can check that whether you URL connection is done properly or not using just if/else statement as below.
if(conn)
{
NSLog(@”Connection Successful”)
}
else
{
NSLog(@”Connection could not be made”);
}
5. To receive the data from the HTTP request , you can use the delegate methods provided by the URLConnection Class Reference. Delegate methods are as below.
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data
Above method is used to receive the data which we get using post method.
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
This method , you can use to receive the error report in case of connection is not made to server.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
The above method is used to process the data after connection has made successfully.

MAPVIEW IN IOS

Map.h

#import <MapKit/MapKit.h>
@interface Map : UIViewController
{
    
}
-(IBAction)back:(id)sender;
@property (nonatomicretainIBOutlet MKMapView *infomap;

@end


Map.m



@interface Map ()

@end

@implementation Map

@synthesize infomap;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [super viewDidLoad];
    [super viewDidUnload];
    [infomap setMapType:MKMapTypeStandard];
    [infomap setZoomEnabled:YES];
    [infomap setScrollEnabled:YES];
    
    MKCoordinateRegion region = { {0.00.0 }, {0.00.0 } };
    region.center.latitude = 13.069613;
    region.center.longitude = 80.273817;
    region.span.longitudeDelta = 0.01f;
    region.span.latitudeDelta = 0.01f;
    [infomap setRegion:region animated:YES];
    
    MainClass *ann = [[MainClass allocinit];
    ann.title =  @"Selvi's";
    ann.subtitle =  @"Tamil Nadu Legislative Assembly";
    
    ann.coordinate = region.center;
    [infomap addAnnotation:ann];
    
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(IBAction)back:(id)sender
{
    contactus *second = [[contactus allocinitWithNibName:nil bundle:nil];
    second.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self dismissModalViewControllerAnimated:YES];
}
@end

Group tableview


GroupedTableViewController.h

#import <UIKit/UIKit.h>

@interface GroupedTableViewController:  UIViewController<UITableViewDelegateUITableViewDataSource>
{
    NSArray *tableData,*tableData1;
    IBOutlet UITableView *tableView1;
}


@end


GroupedTableViewController.m


#import "GroupedTableViewController.h"

@interface GroupedTableViewControllerViewController ()

@end

@implementation GroupedTableViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    
    //self.navigationItem.title = @"Title";
    // self.navigationItem.leftBarButtonItem = self.editButtonItem;
    tableData = [[NSArray alloc]initWithObjects@"Cab Operators ",@"Package Tours",@"Group Vehicles",@"Rate Card",nil];
    tableData1 = [[NSArray alloc]initWithObjects@"Package Tours",@"Cab Operators",@"Group Vehicles",@"Rate Card",nil];
    tableView1.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleHeight;
    
    //NSLog(@"%d",tableData.count);
    
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
#pragma mark - Table view data source

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 50.0f;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return tableData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier; //= @"Cell";
     
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  

    
    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell allocinitWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        
           }
     NSUInteger row = [indexPath row];
    
    if (indexPath.section==0)
    {
        cell.textLabel.font=[UIFont systemFontOfSize:14];
        cell.textLabel.textAlignment=NSTextAlignmentLeft;
        //[cell setSelectionStyle:UITableViewCellEditingStyleNone];
        
        cell.textLabel.text = [tableData objectAtIndex:row];
        
    }
    else 
    {
       /* UILabel *description=[[UILabel alloc]initWithFrame:CGRectMake(100, 0, 210,44 )];
        description.text=[NSString stringWithFormat: @"%@",[tableData1 objectAtIndex:indexPath.row]];
        [description setFont:[UIFont systemFontOfSize:14]];
        description.textColor=[UIColor blackColor];
        
        description.backgroundColor=[UIColor clearColor];
        //description.textColor=[UIColor colorWithRed:131.0f/255.0f green:15.0f/255.0f blue:19.0f/255.0f alpha:1];
        description.numberOfLines=3;
        [cell.contentView addSubview:description];*/
        cell.textLabel.font=[UIFont systemFontOfSize:14];
        cell.textLabel.textAlignment=NSTextAlignmentLeft;
        //[cell setSelectionStyle:UITableViewCellEditingStyleNone];
        
        cell.textLabel.text = [tableData1 objectAtIndex:row];
        

    }

        
     tableView.backgroundColor=[UIColor clearColor];
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSLog(@"Tested");
    }


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end




delegates in Objective C

What is a Delegate?
Technically speaking, a delegate is just an object that has been assigned by another object as the object responsible for handling events.

An example with real time situation:
A xyz office has some 5 counters, you will enter into office go to counter 1 and submit the some details etc. And the counter 1 will tell you to wait for some time and move to counter 3 to finish to get your work complete. In this case counter 3 is your delegate which handles your events, which is referred by counter 1. Here the same counter 1 may also be the delegate, to handle your events.

Why Delegate is required? 
In one word we can define delegate as “Call-back”. Means after performing some task, who, why and how will handle the events.

What is the use of this Delegate? 
While building any applications or projects delegates play a very important roles. In some situations some tasks should be performed out of the area and after finishing we should get to know the results, whether task is achieved or in progress or stopped. These statuses can be accessed by using the delegates.

Predefined Example:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“Hello” message:@“This is an alert view” delegate:self cancelButtonTitle:@“OK” otherButtonTitles:nil];

The initializer of the UIAlertView class includes a parameter called the delegate. Setting this parameter to self means that the current object is responsible for handling all the events fired by this instance of the UIAlertView class. If you don’t need to handle events fired by this instance, you can simply set it to nil:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“Hello”message:@“This is an alert view” delegate:nil cancelButtonTitle:@“OK” otherButtonTitles:nil];

If you have multiple buttons on the alert view and want to know which button was tapped, you need to handle the methods defined in the UIAlertViewDelegate protocol. You can either implement it in the same class in which the UIAlertView class was instantiated or create a new class to implement the method, like this:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog([NSString stringWithFormat:@“%d”, buttonIndex]);
}

To ensure that the alert view knows where to look for the method, create an instance of SomeClass and then set it as the delegate:

SomeClass *myDelegate = [[SomeClass alloc] init];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“Hello” message:@“This is an alert view” delegate:myDelegate cancelButtonTitle:@“OK” otherButtonTitles:@“Option 1”, @“Option 2”, nil];
[alert show];


How to create our own delegates?
To create your own delegate, first you need to create a protocol and declare the necessary methods, without implementing. And then implement this protocol into your header class where you want to implement the delegate or delegate methods.

A protocol must be declared as below:

@protocol ServiceResponceDelegate <NSObject>

- (void) serviceDidFailWithRequestType:(NSString*)error;
- (void) serviceDidFinishedSucessfully:(NSString*)success;

@end

This is the service class where some task should be done. It shows how to define delegate and how to set the delegate. In the implementation class after the task is completed the delegate's the methods are called. 

@interface ServiceClass : NSObject
{
id <ServiceResponceDelegate> _delegate;
}

- (void) setDelegate:(id)delegate;
- (void) someTask;

@end


@implementation ServiceClass

- (void) setDelegate:(id)delegate
{
_delegate = delegate;
}

- (void) someTask
{
...
...
...
perform task
...
...
...
if (!success)
{
[_delegate serviceDidFailWithRequestType:@”task failed”];
}
else
{
[_delegate serviceDidFinishedSucessfully:@”task success”];
}
}
@end

This is the main view class from where the service class is called by setting the delegate to itself. And also the protocol is implemented in the header class.

@interface viewController: UIViewController <ServiceResponceDelegate>
{
ServiceClass* _service;
}

- (void) go;

@end



@implementation viewController

//
//some methods
//

- (void) go
{
_service = [[ServiceClass alloc] init];
[_service setDelegate:self];
[_service someTask];
}


That's it, and by implementing delegate methods in this class, control will come back once the operation/task is done.