其最主要的想法是把帳號和密碼存在CoreData裡面...
一開始先讓使用者去增加.. 存在CoreData...
然後接下來再去查找CoreData.. 看看有沒有剛剛的username 和相對應的password...
如果沒有符合的username -> 請使用者創建新的帳密
有符合的username, 在看密碼對不對...再去判斷...
介面外觀如上.. 有輸入帳號和密碼的 textField.. 還有下面紅色的按鈕....
先來看新增個人帳號的地方
var isExisted:Bool?
@IBOutlet weak var accountTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var loginOutlet: UIButton!
@IBAction func loginAction(_ sender: UIButton) {}
@IBAction func createAction(_ sender: UIButton) {
ceateAlert()
}
當按下 新增個人帳號 的按鈕 -> @IBAction func createAction(_ sender: UIButton)
將會呼叫 ceateAlert()
ceateAlert()的定義如下
func ceateAlert(){
// 新初始化一個 UIAlertController
let alert = UIAlertController(title: "創建帳號", message: "請輸入你的帳號和密碼", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "取消", style: .cancel) { (cation) in
self.dismiss(animated: true, completion: nil)
}
// 增加兩個 UIAlertAction, 創建和取消
let createAction = UIAlertAction(title: "創建", style: .default) { (create) in
//print("create has been pressed")
if alert.textFields?[0].text == "" || alert.textFields?[1].text == "" {
print("Please input your username or password")
}else{
let user = alert.textFields?[0].text
let pwd = alert.textFields?[1].text
self.createuser(username: user!, password: pwd!)
}
}
// 加入兩個textField , 帳號和密碼
alert.addTextField { (textField) in
textField.placeholder = "帳號"
}
alert.addTextField { (textField) in
textField.placeholder = "密碼"
textField.isSecureTextEntry = true
}
// 加上action
alert.addAction(cancelAction)
alert.addAction(createAction)
// 呈現
present(alert, animated: true, completion: nil)
}
介面如下:
當使用者按下創業時
會先去判斷 這兩個文字的內容是不是空的
如果不是.. 就會呼叫 createuser()
if alert.textFields?[0].text == "" || alert.textFields?[1].text == "" {
print("Please input your username or password")
}else{
let user = alert.textFields?[0].text
let pwd = alert.textFields?[1].text
self.createuser(username: user!, password: pwd!)
}
createuser()的定義如下
這裡就會用到Coredata....
先不用管 fetchAccount(), 他在這邊的用意是判斷有沒有已經存在的帳號
func createuser(username:String,password:String){
self.isExisted = false
self.fetchAccount(username: username) { (isExisted,array) in
if isExisted {
print("The user already existed")
} else {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let newUser = NSEntityDescription.insertNewObject(forEntityName: "Users", into: context)
newUser.setValue(username, forKey: "username")
newUser.setValue(password, forKey: "password")
do{
try context.save()
}catch{
print("Could not save")
}
}
}
}
重點是這邊
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let newUser = NSEntityDescription.insertNewObject(forEntityName: "Users", into: context)
newUser.setValue(username, forKey: "username")
newUser.setValue(password, forKey: "password")
do{
try context.save()
}catch{
print("Could not save")
}
再來看 fetchAccount()
這邊的用意是說.. 去存取CoreData
使用 request.predicate = NSPredicate(format: "username = %@", username)
去找 使用者輸入為username 的String....
找到後,把他相對應的password把他回傳出去...
func fetchAccount(username:String, completionHandler: @escaping (_ isExisted:Bool,_ userDictionary: NSArray) -> ()){
var array = [NSDictionary]()
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Users")
request.predicate = NSPredicate(format: "username = %@", username)
request.returnsObjectsAsFaults = false
do{
// fetch result
// fetch result
let results = try context.fetch(request)
// 如果count 大於0, 代表有
if results.count > 0 {
// 使用for loop 去確定每一個結果
// 使用for loop 去確定每一個結果
for result in results as! [NSManagedObject]{
// use result.value(forked: ":)
// use result.value(forked: ":)
if let user = result.value(forKey: "username") as? String{
let pws = result.value(forKey: "password") as! String
// 創建一個新的NSDictionary
// 創建一個新的NSDictionary
let userDict = ["username":user, "password":pws]
// 增加到array
// 增加到array
array.append(userDict as NSDictionary)
// 代表找到了
// 代表找到了
self.isExisted = true
}
}
}
}catch{
print("fetch failure")
}
// 回傳出去
completionHandler(self.isExisted!,array as NSArray)
}
最後再來看 , 大概就是這樣
@IBAction func loginAction(_ sender: UIButton) {
if accountTextField.text! == "" || passwordTextField.text! == ""{
print("Please input your username or password")
}else{
isExisted = false
fetchAccount(username: accountTextField.text!, completionHandler: { (isExisted, userDictionary) in
if isExisted {
let password = (userDictionary[0] as! NSDictionary)["password"]
if(self.passwordTextField.text! == password as! String){
print("login in success")
}else{
print("the password is wrong")
}
}else{
print("The user account you type does not existed, please create a new one")
}
})
}
}
沒有留言:
張貼留言