This button action below allows the user when logging with the two textfields emailLogin.text, passwordLogin.text to go to a web view and display login successful with the web servers data. Using a restful web service through a Post method.I want to convert this code to swift to allow me to do the same thing.
- (IBAction)btnLogin:(id)sender {
//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;
[data release];
//initialize url that is going to be fetched.
NSURL *url = [NSURL URLWithString:@"http://192.168.1.9/phploginws/index.php"];
//NSURL *url = [NSURL URLWithString:@"http://192.168.1.9/MyWebService.php"];
//initialize a request from url
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[url standardizedURL]];
//set http method
[request setHTTPMethod:@"POST"];
//initialize a post data
NSString *postData = [NSString stringWithFormat:@"tag=login&email=%@&password=%@", emailLogin.text, passwordLogin.text];
//NSString *postData = [[NSString alloc] initWithString:@"tag=login&email=dan@test.com&password=12345"];
//NSString *postData = [[NSString alloc] initWithString:@"rw_app_id=1&code=test&device_id=test"];
//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;
[connection release];
//start the connection
[connection start];
}