Skip to content

Instantly share code, notes, and snippets.

@riccardopirani
Created June 6, 2017 18:28
Show Gist options
  • Save riccardopirani/66cf897d206857e991c820df8a230b93 to your computer and use it in GitHub Desktop.
Save riccardopirani/66cf897d206857e991c820df8a230b93 to your computer and use it in GitHub Desktop.
Error on unwrapping string email and password
//
// User.swift
// GeneratorPass
//
// Developer: http://riccardopirani.altervista.org
// Created by riccardo
// Copyright © 2016 ViewSoftware. All rights reserved.
// Software House: http://viewsoftware.it
import Foundation
class User{
//URL to our web service
let URL_SAVE_TEAM = "http://localhost/test/login_mobile.php"
var email:String?=""
var password:String?=""
func PrintValue(){
// print(username);
//print(password);
}
func Login() -> String{
let emailwrap=email!
let pwdwrap=password!
var ris="";
//created URL
guard let requestURL = URL(string: URL_SAVE_TEAM) else { return "no" }
//creating URLRequest
var request = URLRequest(url: requestURL)
//setting the method to post
request.httpMethod = "POST"
//getting values from text fields
//creating the post parameter by concatenating the keys and values from text field
let postParameters = "email=\(emailwrap)&password=\(pwdwrap)"
//adding the parameters to request body
request.httpBody = postParameters.data(using: .utf8)
//creating a task to send the post request
let session = URLSession.shared
let task = session.dataTask(with: request) {
data, response, error in
guard error == nil else {
print("error is \(error!.localizedDescription)")
return
}
guard let data = data else {
print("No data was returned by the request!")
return
}
//parsing the response
do {
//converting resonse to NSDictionary
let myJSON = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? Dictionary<String, String?>
//parsing the json
guard let parseJSON = myJSON, let msg = parseJSON["message"] as? String else {
print("Error parsing data")
return
}
//printing the response
print("Responso di ritorno: "+msg)
ris=msg;
} catch {
print(error)
}
}
//executing the task
task.resume()
return ris;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment