-3

I need to pass the username and password to a url using post method.Also I want to get the response back from the service.can anybody please tell the efficient way to do this?

Shaan
  • 179
  • 1
  • 1
  • 8
  • what have you tried so far? a simple form should do... without more information (especially info on what you tried already) it's impossible to help – codeling Nov 22 '13 at 07:19

3 Answers3

1

data not post through webservice json in ios

Check out this link for the answer posted by me in regards to the question same as that of your question. Hope it will help you .

Community
  • 1
  • 1
Gaurav Rastogi
  • 2,145
  • 1
  • 14
  • 19
1

add this code on .h file

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

add this code on .m file @implementation........

@synthesize receivedData,connection;
-(void)requesttoserver
{

    //if there is a connection going on just cancel it.
    [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:@"https://www.your urlname"];

    //initialize a request from url
    request = [NSMutableURLRequest requestWithURL:[url standardizedURL]];

    //set http method
    [request setHTTPMethod:@"POST"];

    //initialize a post data

    NSString *postData = @"username password";


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


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

    //set post data of request
    [request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];


    //initialize a connection from request
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    self.connection = connection;
    //[connection release];

    //start the connection
    [connection start];


}


-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [self.receivedData appendData:data];

}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{

    NSLog(@"error%@" , error);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSString *htmlSTR = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding];





}

call to requesttoserver method when you want to post data

Sport
  • 8,570
  • 6
  • 46
  • 65
1

I tried this one

NSString *Post=[NSString stringWithFormat:@"email=%@&password=%@&password_confirmation=%@",[textfieldArray objectAtIndex:0],[textfieldArray objectAtIndex:1],[textfieldArray objectAtIndex:2]];

NSData *PostData = [Post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
NSString *PostLengh=[NSString stringWithFormat:@"%d",[Post length]];
NSURL *Url=[NSURL URLWithString: @"http://offers2win.com/api/v1/users/"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:Url     cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
[request setValue:PostLengh forHTTPHeaderField:@"Content-Lenght"];
[request setHTTPBody:PostData];

NSData *ReturnData =[NSURLConnection sendSynchronousRequest:request returningResponse:Nil error:Nil];

NSString *Response = [[NSString alloc] initWithData:ReturnData encoding:NSUTF8StringEncoding];

//Response = [Response stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

NSLog(@"Response%@",Response);
Gaurav Rastogi
  • 2,145
  • 1
  • 14
  • 19
Shaan
  • 179
  • 1
  • 1
  • 8
  • 2
    If this is attempted code please add it in your question and not as an answer. If this code is a solution please mark this as accepted answer. – Amar Nov 22 '13 at 08:00