Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "ViewController.h"
- @interface ViewController ()
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- NSString *post = [NSString stringWithFormat:@"name=%@&email=%@&password=%@&password_confirmation=%@",@"motmao",@"motmao@gmail.com",@"123456",@"123456"];
- NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
- NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
- NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
- [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://manh-nt.herokuapp.com/users.json"]]];
- [request setHTTPMethod:@"POST"];
- [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
- [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
- [request setHTTPBody:postData];
- NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
- if(conn)
- {
- NSLog(@"Connection Successful");
- }
- else
- {
- NSLog(@"Connection could not be made");
- }
- }
- // This method is used to receive the data which we get using post method.
- - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data {
- NSString* myString;
- myString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
- NSLog(@"%@",myString);
- }
- // 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 {}
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement