从mysql数据库添加登录页面到swift

时间:2018-03-25 05:40:03

标签: mysql swift xcode9

我正在尝试将网站的登录数据库实施到一个快速的应用程序,但我似乎无法找到如何这样做,我在网上找到的所有东西都没有真正帮助。该网站将登录数据存储在mySQL数据库中,我想将其实现到我的应用程序中。

这是我的代码:

import UIKit

class SignInViewController: UIViewController,UITextFieldDelegate {

@IBOutlet var UsernameTextField: UITextField!
@IBOutlet var PasswordTextField: UITextField!
@IBOutlet var LogInButton: UIButton!


override func viewDidLoad() {
    super.viewDidLoad()
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    navigationController?.navigationBar.shadowImage = UIImage()
    navigationController?.navigationBar.isTranslucent = true
    navigationController?.view.backgroundColor = UIColor.clear
    // Remove Autocorrection Type
    UsernameTextField.autocorrectionType = .no
    PasswordTextField.autocorrectionType = .no
    PasswordTextField.textContentType = UITextContentType("")
    //Next button takes user to the next textfield
    UsernameTextField.delegate = self
    PasswordTextField.delegate = self
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
@IBAction func LogInButtonTapped(_ sender: Any) {
// `READ VALUES FROM TEXTFIELDS
    let username = UsernameTextField.text
    let password = PasswordTextField.text

// CHECK IF BOTH FIELDS ARE EMPTY
    if (username?.isEmpty)! && (password?.isEmpty)! {
// DISPLAY ALERT MESSAGE HERE
        print("User name \(String(describing: username)) or password \(String(describing: password)) is empty")
        displayMessage(userMessage: "Both fields are required to fill in")

        return
    } else if (password?.isEmpty)! {
        displayMessage(userMessage: "You did not enter a passwords")
        return
    } else if (username?.isEmpty)! {
        displayMessage(userMessage: "You did not enter a username")
        return
    }
// CREATE ACTIVITY INDICATOR
    let myActivityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
// POSITION ACTIVITY INDICATOR IN CENTER OF THE VIEW
    myActivityIndicator.center = view.center
// START ACTIVITY INDICATOR
    myActivityIndicator.startAnimating()

    view.addSubview(myActivityIndicator)



 }


func displayMessage(userMessage:String) -> Void {
    DispatchQueue.main.async
        {
            let alertController = UIAlertController(title: "Alert", message: userMessage, preferredStyle: .alert)

            let OKAction = UIAlertAction(title: "OK", style: .default) { (action:UIAlertAction!) in
                // Code in this block will trigger when OK button tapped.
                DispatchQueue.main.async
                    {
                        self.dismiss(animated: true, completion: nil)
                }
            }
            alertController.addAction(OKAction)
            self.present(alertController, animated: true, completion:nil)
    }
}

//Remove keyboard on tap of screen and Go to next textfield everytime user taps on next
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.view.endEditing(true)
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    if textField == UsernameTextField{
        PasswordTextField.becomeFirstResponder()

} else if textField == PasswordTextField {
        self.view.endEditing(true)
    }
    return true
  }
func removeActivityIndicator(activityIndicator: UIActivityIndicatorView)
{
    DispatchQueue.main.async
        {
            activityIndicator.stopAnimating()
            activityIndicator.removeFromSuperview()
    }
}

}

如何将mysql数据库中的登录信息添加到应用程序中?我的目标是为网站创建一个应用程序,我可以使用与该网站相同的用户名和密码。

1 个答案:

答案 0 :(得分:2)

您必须为应用程序创建API服务以发送用户名和密码,并且如果登录有效,您的后端将验证。 查看REST / JSON服务。