0

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];

}
Christian
  • 22,585
  • 9
  • 80
  • 106
Dan A
  • 209
  • 1
  • 3
  • 12

2 Answers2

0

I recommend you to check the Objective-C/Swift documentation Apple provides.

Apple has a documentation for everything they provide in both languages. To check the differences, just open a documentation of, for example NSMutableDictionary. Then you can choose the language you need:

enter image description here

But to answer you original question, you can basically use every class in your Objective-c code also in Swift. Only the Syntax is different:

self.connection.cancel()

var data = NSMutableData()

self.receivedData = data

var url = NSURL(string: "http://192.168.1.9/phploginws/index.php")

var request = NSMutableURLRequest(URL: url!.standardizedURL!)

request.HTTPMethod = "POST"

var postData = "tag=login&email=\(emailLogin.text)&password=\(passwordLogin.text)"

request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")

request.HTTPBody = postData.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)

var connection = NSURLConnection(request: request, delegate: self)

self.connection = connection
Christian
  • 22,585
  • 9
  • 80
  • 106
  • Thanks, for some reason I can't get the code to post the information of the web service on the webview. It would be great help if you could use that same workable code to put on a project and allow it to get back the information of the web service once the person logs in the email and password then hits login.Then put it on github for me and other like me that struggle with this. – Dan A Feb 20 '15 at 03:48
  • I'm sorry. But that's a bit sad actually. That is the exact code you've posted just in Swift. Normally, SO isn't a place where code is written for you but where we help you by fixing problems. As your code is copy pasted from an answer(http://stackoverflow.com/questions/12358002/submit-data-to-google-spreadsheet-form-from-objective-c) I would recommend you to understand it first, before you go further. I've posted a link to the documentation in my answer so that you can check if anything doesn't work. Please put some time in learning Swift. – Christian Feb 20 '15 at 16:24
0

This piece of code does the exact same thing. Hope it helps :)

self.connection?.cancel()
var data = NSMutableData()
self.receivedData = data
var request = NSMutableURLRequest(URL: NSURL(string: "http://192.168.1.9/phploginws/index.php")!)
var postDataString = "tag=login&email=\(emailLogin.text)&password=\(passwordLogin.text)"
var postData:NSData = postDataString.dataUsingEncoding(NSASCIIStringEncoding)!
request.HTTPMethod = "POST"
request.HTTPBody = postData
request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
var connection = NSURLConnection(request: request, delegate: self)
self.connection = connection
connection?.start()
DanielEdrisian
  • 716
  • 1
  • 4
  • 17