Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAbdul Manaf
34 Pointsi wrote one project for username and password service call but i can't write username and password in login page
this is one of the login project it contains two view controller, viewcontroller and secondviewcontroller. i can't write username and password at the moment of login
// Enter your code belo//
// ViewController.swift
// Dashboard1
//
// Created by Mac book Pro on 28/02/18.
// Copyright © 2018 sinnonteq. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var memberLogin: UILabel!
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var passwordField: UITextField!
@IBOutlet weak var loginButton: UIButton!
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
var secondController = segue.destination as! SecondViewController
secondController.myString = textField.text!
}
override func viewDidLoad()
{
let preferences = UserDefaults.standard
if(preferences.object(forKey: "session") != nil)
{
LoginDone()
}
else
{
LoginToDo()
}
}
@IBAction func loginButton(_ sender: Any)
{
if textField.text != ""
{
performSegue(withIdentifier: "segue", sender: self)
if(loginButton.titleLabel?.text == "Logout")
{
let preferences = UserDefaults.standard
preferences.removeObject(forKey: "session")
LoginToDo()
return
}
let username = textField.text
let password = passwordField.text
if(username == "" || password == "")
{
return
}
DoLogin(username!, password!)
}
}
func DoLogin(_ user:String, _ psw:String)
{
let url = URL(string: "https://cpanel.q-hawk.com/adminPanel/login.php")
let session = URLSession.shared
let request = NSMutableURLRequest(url: url!)
request.httpMethod = "POST"
let paramToSend = "username=" + user + "&password=" + psw
request.httpBody = paramToSend.data(using: String.Encoding.utf8)
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(data, response, error) in
guard let _:Data = data else
{
return
}
let json:Any?
do
{
json = try JSONSerialization.jsonObject(with: data!, options: [])
}
catch
{
return
}
guard let server_response = json as? NSDictionary else
{
return
}
if let data_block = server_response["data"] as? NSDictionary
{
if let session_data = data_block["session"] as? String
{
let preferences = UserDefaults.standard
preferences.set(session_data, forKey: "session")
DispatchQueue.main.async (
execute:self.LoginDone
)
}
}
})
task.resume()
}
func LoginToDo()
{
textField.isEnabled = true
passwordField.isEnabled = true
loginButton.setTitle("Login", for: .normal)
}
func LoginDone()
{
textField.isEnabled = false
passwordField.isEnabled = false
loginButton.setTitle("Logout", for: .normal)
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
self.view.endEditing(true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool
{
textField.resignFirstResponder()
return(true)
}
}
w