Friday 2 October 2015

Post method to send request And Response In Ios /Iphone

 ==> Here Usd some IBOUTLET Of NSUrlCOnnection and
==> Used Some NsmutableData Object Using it Work it.

@property (retain, nonatomic) NSURLConnection *connection;
@property (retain, nonatomic) NSMutableData *receivedData;


==> Use this method to post some

-(void)postMethod
{
    [self.connection cancel];

    //initialize new mutable data

    NSMutableData *data = [[NSMutableData alloc] init];

    self.receivedData = data;

    //initialize url that is going to be fetched.

    NSURL *url = [NSURL URLWithString:@"http://192.168.0.176/idictionary/index.php?r=login"];

    //initialize a request from url

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[url standardizedURL]];

    //set http method

    [request setHTTPMethod:@"POST"];

==> Here  data whitch Using Post Data

    NSString *postData = @"mac_id=val2&email_id=abc@gmail.com&password=abcfggkjhkj";


==>Here You Heve to Give To Some Parameter Vakue That You Want:

    //set request content type we MUST set this value.

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

    //set post data of request

    [request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];

    //initialize a connection from request

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

    self.connection = connection;
    //start the connection

    [connection start];

}
==> Add Some Deleget file Of Method And Action Which Help it.

/*

=> this method might be calling more than one times according to incoming data size
 */

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

    [self.receivedData appendData:data];
}
/*

 if there is an error occured, this method will be called by connection
 */

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"%@" , error);
}
/*

==>  if data is successfully received, this method will be called by connection

 */
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{

    //initialize convert the received data to string with UTF8 encoding

    NSString *htmlSTR = [[NSString alloc] initWithData:self.receivedData

                                              encoding:NSUTF8StringEncoding];

   // NSLog(@"%@" , htmlSTR);
    NSError *e = nil;
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: self.receivedData options:
    NSJSONReadingMutableContainers error: &e]; <== Here used One Parser Called   SBJson Prser

    NSLog(@"data %@",jsonArray);  <== here Some OutPut Display

    //show controller with navigation
}