我有登录页面如何使用 coredata 数据库对用户进行身份验证?

Posted

技术标签:

【中文标题】我有登录页面如何使用 coredata 数据库对用户进行身份验证?【英文标题】:I am having login page how to authenticate the user using coredata database? 【发布时间】:2017-04-21 06:31:55 【问题描述】:

我已经创建了一个登录页面和注册页面,并将注册数据保存在核心数据数据库中,但现在我需要对用户进行身份验证,以便移动到另一个视图控制器。

这是我的代码....

import UIKit
import CoreData
class ViewController: UIViewController 
@IBOutlet weak var usernameTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var loginButton: UIButton!
@IBOutlet weak var registerButton: UIButton!
var accounts = [Account]()
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

override func viewDidLoad() 
    super.viewDidLoad()
    self.getAccountData()
    loginButton.layer.cornerRadius = 12.0;
    registerButton.layer.cornerRadius = 12.0;
    usernameTextField.layer.borderWidth = 1;
    usernameTextField.layer.borderColor = UIColor.darkGray .cgColor;
    usernameTextField.layer.cornerRadius = 5;
    usernameTextField.setValue(UIColor.lightGray, forKeyPath: "_placeholderLabel.textColor");
    passwordTextField.layer.borderWidth = 1;
    passwordTextField.layer.borderColor = UIColor.darkGray.cgColor;
    passwordTextField.layer.cornerRadius = 5;
    passwordTextField.setValue(UIColor.lightGray, forKeyPath: "_placeholderLabel.textColor");
    // Do any additional setup after loading the view, typically from a nib.


@IBAction func registerButton(_ sender: Any) 
    self.performSegue(withIdentifier: "SecondViewController", sender: nil)
    return

override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
    _ = segue.destination as! SecondViewController


@IBAction func loginButton(_ sender: Any) 
    if (usernameTextField.text?.isEmpty)! == true
    
        let usernamealert = UIAlertController(title: "Warning", message: "Please enter username", preferredStyle: UIAlertControllerStyle.alert)
        usernamealert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
        self.present(usernamealert, animated: true, completion: nil)
    
    if (passwordTextField.text?.isEmpty)! == true 
            let passwordalert = UIAlertController(title: "Warning", message: "Please enter password", preferredStyle: UIAlertControllerStyle.alert)
            passwordalert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
            self.present(passwordalert, animated: true, completion: nil)
    
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let managedContext = appDelegate.persistentContainer.viewContext
    let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Account")
    do
    
        let results = try context.fetch(request)
        if results.count > 0
        
            for result in results as! [NSManagedObject]
            
                if let firstname = result.value(forKey: "firstName") as? String
                
                    if(usernameTextField.text == firstname)
                    
                        if let password = result.value(forKey: "lastname") as? String
                        
                            if(passwordTextField.text == password)
                            

                            else
                                let passwordalert = UIAlertController(title: "Warning", message: "Wrong password", preferredStyle: UIAlertControllerStyle.alert)
                                passwordalert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
                                self.present(passwordalert, animated: true, completion: nil)
                            
                        
                    
                
            
        
    

@IBAction func unwindToView(segue: UIStoryboardSegue)
func getAccountData() 
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    do 
        accounts = try context.fetch(Account.fetchRequest())
     catch  
        print("Fetching Failed")
    

【问题讨论】:

欢迎来到 ***,您面临的问题是什么,请解释问题,请阅读如何在本站提问,谢谢 【参考方案1】:

试试这样 从 viewWillAppear 调用此方法

self.getAccountData()

验证完成后,将下面的代码放入 loginButton() 方法中

 var username = ""
 var passString = ""
 var checkRecord = false
 for item in accounts
 
    username = item.value(forKeyPath: "firstName") as! String
    passString = item.value(forKeyPath: "lastname") as! String
    if username == usernameTextField.text && passwordTextField.text == passString
    
         checkRecord = true;
    
 
 if checkRecord == true
 
     //succesfull authenticate
 
 else
 
   //show the error message
 

【讨论】:

你能告诉我如何传递 5 个字符串吗?

以上是关于我有登录页面如何使用 coredata 数据库对用户进行身份验证?的主要内容,如果未能解决你的问题,请参考以下文章

更改 App 当前的 CoreData 源

如何跨视图传递 CoreData 对象

如何将 fmdb 用于登录页面?

页面刷新后如何保持用户登录

如何强制更新 CoreData SQLite 数据库?

如何使用 coreData 过滤数据