0

I am working on a student system project. All the server side things are done but the iOS client application is making some trouble.

I am trying to create a login page for the user and than check in the database whether the info is correct

Here is the code:

import Foundation
import UIKit

class LoginPage: UIViewController {

    /*A reference to the username/student ID text field*/
    @IBOutlet weak var idField: UITextField!

    //A reference to the password field
    @IBOutlet weak var passwordField: UITextField!

    //Send the information from the text fields to the server
    @IBAction func loginButtonTapped(_ sender: UIButton) {

        //declare parameter as a dictionary which contains string as key and value combination.
        var parameters = ["name": idField.text!, "password": passwordField.text!] as Dictionary<String, String>

        //create the url with NSURL
        let url = NSURL(string: "https://api.sis.kemoke.net")

        //create the session object
        var session = URLSession.sharedSession()

        //now create the NSMutableRequest object using the url object
        let request = NSMutableURLRequest(url: url! as URL)
        request.httpMethod = "POST" //set http method as POST

        var err: NSError?
        request.HTTPBody = JSONSerialization.dataWithJSONObject(parameters, options: nil, error: &err) // pass dictionary to nsdata object and set it as request body

        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        request.addValue("application/json", forHTTPHeaderField: "Accept")

        //create dataTask using the session object to send data to the server
        var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
            println("Response: \(response)")
            var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
            println("Body: \(strData)")
            var err: NSError?
            var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: &err) as? NSDictionary

            // Did the JSONObjectWithData constructor return an error? If so, log the error to the console
            if(err != nil) {
                println(err!.localizedDescription)
                let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding)
                println("Error could not parse JSON: '\(jsonStr)'")
            }
            else {
                // The JSONObjectWithData constructor didn't return an error. But, we should still
                // check and make sure that json has a value using optional binding.
                if let parseJSON = json {
                    // Okay, the parsedJSON is here, let's get the value   for 'success' out of it
                    var success = parseJSON["success"] as? Int
                    println("Succes: \(success)")
                }
                else {
                    // Woa, okay the json object was nil, something went worng. Maybe the server isn't running?
                    let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding)
                    println("Error could not parse JSON: \(jsonStr)")
                }
            }
        })

        task.resume()
    }
}

For me this all looks good, but I am getting an error "can not call function URLSession" and "Extra argument" for the error handler.

How do I fix this?

Rob
  • 415,655
  • 72
  • 787
  • 1,044
TecHummer
  • 139
  • 1
  • 9
  • 3
    You say you're using Swift 3 but you are using swift 2 class names and method signatures all over the place and your error handling code is from way back in Swift 1. You need to use more up-to-date tutorials/resources. – dan Nov 21 '16 at 23:22
  • I'd suggest you search stack overflow for "[swift3] urlsession json" and you'll get tons of hits. E.g. http://stackoverflow.com/a/39861328/1271826 – Rob Nov 21 '16 at 23:29

0 Answers0