Friday 2 October 2015

Use of Username And Pasword Post Method In Ios/Iphone

==> Here,  How can use of POST Method.: And Pass Parameter Username Password:

1. Use Some Like String With Actual Username And Password.

   NSString *post = [NSString stringWithFormat:@"Username=%@&Password=%@",
   @"username",@"password"];

2. PostString  That Encoding using NSASCIIStringEncoding  also

=> You Need Send That Post String To  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. Use Object  a UrlRequest with  Properties Like HTTP Method,

=>  HTTP Header Field With Length of
    Post String. Create URLRequest object and initialize it.

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];

 =>   Set That URL Whitch going to Send The Data To That Request.

     [request setURL:[NSURL URLWithString:

     @"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"];

 => Set Also Here Encoded Value For HTTP Header:-

   [request setValue:@"application/x-www-form-urlencoded"
   forHTTPHeaderField:@"Content-Type"];


=> Set UrlRequest with PostData To HttpBody:-

    [request setHTTPBody:postData];

 4. Create URLConnection object. Initialize it Using With The URLRequest.

   NSURLConnection *conn = [[NSURLConnection alloc]
   initWithRequest:request  delegate:self];

=> It returns the initialized URL connection , load the data for the URL request.
   You can check URL connection Based On Below Code  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.

=>// This method is used to receive the data which

   we get using post method.

- (void)connection:(NSURLConnection *)connection

didReceiveData:(NSData*)data

// This method receives the error report in case of connection is

not made to server.


- (void)connection:(NSURLConnection *)connection

didFailWithError:(NSError *)error

// This method is used to process the data after connection

has made successfully.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection