0

I recently made a single-screen web-based application using PHP and AJAX. I want to port this app to iOS, and have started to teach myself Swift. I had the application working perfectly in Swift 1, but since I updated to xCode 7.2 and Swift 2 so that I could try the app on my iPhone with iOS 9, the code was completely non-functional. From here, I have new code now, and as far as I can see, it should work, but I am getting an empty response. What is wrong?

import UIKit
import Foundation

class LoginViewController: UIViewController, UITextFieldDelegate {



    @IBOutlet weak var username_textField: UITextField!

    @IBOutlet weak var password_TextFieldOutlet: UITextField!


    @IBAction func password_TextFieldAction(sender: AnyObject) {


        let username = username_textField.text
        let password = password_TextFieldOutlet.text
        var urlRequest = "~~URL~~/login.php?username=\(username)&password=\(password)"



    }


    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

The idea is I was to get my data from the database and see if "success" (a boolean value in the db) returns 1 or 0. If it returns 1, then perform a segue to next page, if not, create an alert saying incorrect username and password.

Thank you in advance!

Liam Tan
  • 1
  • 1

2 Answers2

0
  1. Create the url

    let url = NSURL(string: "http://google.com")

  2. Create the request

    let request = NSMutableUrlRequest(url: url!)

  3. Add data and request methods

    request.HTTPMethod = "POST" request.HTTPBody = "username=\(username text.text!)".dataUsingEncoding(NSUTF8StringEncoding)

  4. Send the connection

      NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { 
         (response, data, error) -> Void in
     if data != nil && error == nil {
         if let response = String(data: data!, encoding: NSUTF8StringEncoding) { 
              //here is where you now have your response 
         }
     })
    
pbush25
  • 5,228
  • 2
  • 26
  • 35
0

Use NSURLConnectionDelegate or AFNetworking for networking requests. Following is an implementation using NSURLConnection.

I have implemented LoginViewController class for you. Hook the following loginButtonAction to 'Login/SignIn/Done' button on your UI.

import UIKit
import Foundation
class LoginViewController: UIViewController, NSURLConnectionDelegate {
@IBOutlet weak var username_textField: UITextField!
@IBOutlet weak var password_TextFieldOutlet: UITextField!
@lazy var data = NSMutableData()

override func viewDidLoad() {
    super.viewDidLoad()
}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    startConnection()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func startConnection(){
let username = username_textField.text
    let password = password_TextFieldOutlet.text
    let urlPath: String = "~~URL~~/login.php?username=\(username)&password=\(password)"
    var url: NSURL = NSURL(string: urlPath)
    var request: NSURLRequest = NSURLRequest(URL: url)
    var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)
    connection.start()
}

func connection(connection: NSURLConnection!, didReceiveData data: NSData!){
    self.data.appendData(data)
}

func loginButtonAction(sender: UIButton!){
    startConnection()
}

func connectionDidFinishLoading(connection: NSURLConnection!) {
    var err: NSError
    var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
//Here I am printing the results, you can check the results and based on response Bool you can perform segue to next view controller.
    println(jsonResult)

}
}
Chanchal Raj
  • 4,176
  • 4
  • 39
  • 46