Alright, so for about a few days now, I'm stuck on this problem. I have a Tab Bar Controller and I have a View Controller where users of the app either create of enter their passwords, lets call it the "Pass View".
The passwords are created using keychain method provided by Apple. Ideally I would want the app to first and always pop up the "Pass View" and then if users enter their password correctly then the app segues to the Tab Bar Controller.
Now I can associate an action segue from my enter/create button to the Tab Bar Controller, but if I do that then even if the users enter their passwords incorrectly the segue will still happen..
Link to Image 1 Showing StoryBoard: StoryBoardImage
Link to Image 2 Showing files: files in my project
Here is my code:
import UIKit
class LoginPassViewController: UIViewController {
let MyKeychainWrapper = KeychainWrapper()
let createLoginButtonTag = 0
let loginButtonTag = 1
@IBOutlet weak var passTextField: UITextField!
@IBOutlet weak var loginButton: UIButton!
override func viewDidLoad() {
// 1.
let hasLogin = NSUserDefaults.standardUserDefaults().boolForKey("hasLoginKey")
// 2.
if hasLogin {
loginButton.setTitle("Login", forState: UIControlState.Normal)
loginButton.tag = loginButtonTag
loginButton.hidden = false
} else {
loginButton.setTitle("Create", forState: UIControlState.Normal)
loginButton.tag = createLoginButtonTag
loginButton.hidden = false
}
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func enterApp(sender: AnyObject) {
// 1.
if passTextField.text == "" {
var alert = UIAlertView()
alert.title = "You must enter a password!"
alert.addButtonWithTitle("Oops!")
alert.show()
return
}
// 2.
passTextField.resignFirstResponder()
// 3.
if sender.tag == createLoginButtonTag {
// 4.
let hasLoginKey = NSUserDefaults.standardUserDefaults().boolForKey("hasLoginKey")
// 5.
MyKeychainWrapper.mySetObject(passTextField.text, forKey:kSecValueData)
MyKeychainWrapper.writeToKeychain()
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "hasLoginKey")
NSUserDefaults.standardUserDefaults().synchronize()
loginButton.tag = loginButtonTag
} else if sender.tag == loginButtonTag {
// 6.
if checkLogin(passTextField.text) {
// THIS IS WHERE IF THE USER PASSWORD IS CORRECT THEN THE VIEW SHOULD SWITCH // TO THE TAB BAR CONTROLLER.
} else {
// 7.
var alert = UIAlertView()
alert.title = "Login Problem"
alert.message = "Wrong username or password."
alert.addButtonWithTitle("Foiled Again!")
alert.show()
}
}
}
func checkLogin(password: String ) -> Bool {
if password == MyKeychainWrapper.myObjectForKey("v_Data") as? String {
return true
} else {
return false
}
}
}
答案 0 :(得分:0)
Just don't create segue programmatically, but you can control the execution of your segue. Give them unique identifier then override these methods:
override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {
//depend on the situation, you can decide to perform this segue or not
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//prepare datas before go to next viewcontroller
}