I have made a login form. Fields r email and password. Now i want to POST the data from fields to specific url how it can be done. I'm totally new to IOS. Can anybody help me?? How to do HTTP request and JSON parsing?
Asked
Active
Viewed 1,007 times
-1
-
Possible duplicate of [Sending an HTTP POST request on iOS](http://stackoverflow.com/questions/15749486/sending-an-http-post-request-on-ios) – Harshal Valanda Apr 27 '17 at 10:56
-
are you using REST call ? – jerfin Apr 27 '17 at 10:58
-
2Possible duplicate of [Post data in Objective C using Json](http://stackoverflow.com/questions/9883081/post-data-in-objective-c-using-json) – Himanshu Moradiya Apr 27 '17 at 11:16
-
And you are new to research tools too? Because there are plenty of questions asking how to do it. Also, you may want to give details on your Web API (if there is something specific to it), show what you've tried, etc. – Larme Apr 27 '17 at 12:06
1 Answers
-1
/*********See this**********/
-(void)webServiceCall{
NSString *dataToSend = [NSString stringWithFormat:@"Username=%@&Password=%@“,<userIdEnter Here>,<Password enter here>];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString *Length = [NSString stringWithFormat:@"%d",[postData length]];
[request setURL:[NSURL URLWithString:@“WEBURL”]];
[request setHTTPMethod:@"POST"];
[request setValue:Length forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
// check connection if you want
/*****get response in delegates*******/
- (void)connection:(NSURLConnection *)connection didReceiveResponse:
(NSURLResponse *)response {
// A response has been received, this is where we initialize the instance var you created
// so that we can append data to it in the didReceiveData method
// Furthermore, this method is called each time there is a redirect so reinitializing it
// also serves to clear it
_responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data
{
/**************/
NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
// NSArray* latestLoans = [json objectForKey:@"loans"];
NSLog(@"json: %@", json);
[_responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Error --> %@",error.localizedDescription);
/***************/
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *responseString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];
NSError *error = nil;
id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
// use Result
self.responseData = nil;
}

Ajjjjjjjj
- 669
- 4
- 12
-
-
Seriously I'm not understanding it can u elaborate the code please? Where to write this code how we get response and where to write response delegate? @ajjjjjjjj – omer Apr 28 '17 at 05:43
-
First you have to paste the code in class where you want to call web service. now, call method `webServiceCall` on **login Button** click, where you have to change user_id and password entered by user. After call happened, 'NSUrlConnection' delegate will called(read delegate documentation). After getting response in `connectionDidFinishLoading`. You have to parse data in this way ` id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]; `. After that you can use 'result' – Ajjjjjjjj Apr 28 '17 at 06:02
-
-