I have a piece of parse code that checks to see if a user is logged in:
PFUser.logInWithUsernameInBackground("myname", password:"mypass") {
(user: PFUser!, error: NSError!) -> Void in
if user != nil {
// Do stuff after successful login.
} else {
// The login failed. Check error to see why.
}
}
After the successful login I want to send the user to a new segue. I can't do this in the storyboard because I only want the segue to occur if the conditions are met. Is there anyway to create a segue programmatically?
You can use:
performSegueWithIdentifier("yourSegueIdentifier", sender: nil)
You dont need to "create" a segue programmatically, just connect your ViewControllers and create a Segue. With performSegueWithIdentifier you can call your segue manually if needed.
Related
I am attempting to authenticate user sign in using Firebase. If a user's credentials are verified, I am seguing to the home screen of my application within an if-else statement using user and error. However, performSegue(...) executes whether or not the log in actually occurred.
I am certain that the problem is not with verification/login/logout issues in Firebase or with the textfields.
Here is my code:
func handleSignIn(username:String, password:String) {
Auth.auth().signIn(withEmail: username, password: password) { user, error in
if error == nil && user != nil {
//self.dismiss(animated: false, completion: nil)
print("Logged in!")
self.performSegue(withIdentifier: "toHome", sender: nil)
} else {
print("Error logging in: \(error!.localizedDescription)")
}
}
}
Best,
Code Daddy
I have solved this issue.
The code above is actually correct and functional. The issue was that the handleSignIn method was called within:
#IBAction func didTapLogin(_ sender: Any) {...}
That button itself was the segue connection with the identifier "toHome," and so regardless of what the if/else block evaluated to, the application proceeded to the home menu.
Once I deleted and reestablished the segue "toHome" from the LoginViewController to the HomeViewController (not the Log In button to the HomeViewController), this code worked properly.
Maybe someone can help, just want to use Segue for Login button to made a transfer to second ViewController only after authorization with Touch ID but application still performing the Segue after user pressing on the button.
import UIKit
import LocalAuthentication
class LoginWindowViewController: UIViewController {
#IBAction func loginButton(_ sender: Any) {
let context: LAContext = LAContext()
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil){
context.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: "For login you need to use your TouchID", reply: {(wasSuccessful, error) in if wasSuccessful {
DispatchQueue.main.async {
self.shouldPerformSegue(withIdentifier: "LoginComplete", sender: self.navigationController)
}
}else{
print ("Bad TouchID")
}
})
}
}
Thanks
Try implementing this function and checking from where it's being called:
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
}
Remember that as you're calling the segue from your code, if you have already added a segue action from your button to the next ViewController in the storyboard you should remove it and only create a connection between your LoginViewController to the one you want to call
As the documentation states, if you do not override the shouldPerformSegue method, the default implementation returns true for all segues.
The shouldPerformSegue(withIdentifier:sender:) method should determine whether a segue with the specified identifier will or will not be called depending on the return value of your implementation.
Since you already check whether the TouchID authorization was successful, you don't actually need to call shouldPerformSegue, but rather you need to call performSegue.
context.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: "For login you need to use your TouchID", reply: {(wasSuccessful, error) in
if wasSuccessful {
DispatchQueue.main.async {
self.performSegue(withIdentifier: "LoginComplete", sender: self.navigationController)
}
}else{
print ("Bad TouchID")
}
})
In swift, how do I only perform a segue after a server has given me a certain response?
I'd like to click a submit button. That will send a request to the server. Then, if the server responds positively, I want a view to be presented modally, otherwise, I want to write a UIAlertView.
I know how to write the UIAlertView and I have the Modal View prepared, I just can't figure out how to write it such that the modal view only presents itself conditionally.
First you need to give a segue identifier on your storyboard. Then you can use:
self.performSegueWithIdentifier("yourSegueIdentifier", sender: nil)
first make a segue between the view controllers and give the segue identifier
eg: self.performSegueWithIdentifier("SegueIdentifier", sender: nil)
and when you get the response of the service request and on the basis of that check the status or the valid http status code.
eg: if status returns true {
self.performSegueWithIdentifier("SegueIdentifier", sender: nil)
}
or if statuscode == 200...299 {
self.performSegueWithIdentifier("SegueIdentifier", sender: nil)
}
Assuming you have your submit button wired up to an IBAction you could do something like this...
#IBAction func submitClicked(sender: UIButton) {
//try to hit server
let serverResponse = server.trySomeFunction()
if serverResponse {
//server response was successful!
self.performSegueWithIdentifier("SegueIdentifier", sender: self)
} else {
//server response failure, present alert
}
}
I am trying to bypass a sign-in view if the user has already signed in once. I have run tests and know I'm getting to the right code. But the segue is never executed.
//bypass the sign in screen if user has already signed in
let signInCheck = NSUserDefaults.standardUserDefaults().valueForKey("editProfile")
if (signInCheck! .isEqual(1)) {
self.performSegueWithIdentifier("loggedIn2", sender: nil)
} else {
NSUserDefaults.standardUserDefaults().setValue(1, forKey: "editProfile")
}
Any help would e appreciated.
I am working for the first time on storyboard using Swift.
In the first page of the app user will login and when login is done successfully, user will navigate to next page.
But When button is clicked firstly navigation occurs then after web service gets called, but what I want is to authenticate the Login Web service first then navigate with login data to next page.
I have put the identifier on the login button i.e, "login_success" on the button in storyboard and called self.performSegueWithIdentifier, when login is successfull.
Please guide me. Thanks.
You can add segue in the identifier in storyboard. Set the segue in the storyboard from File Owner. And when we get the response from the server, then using,
dispatch_async(dispatch_get_main_queue(), {
if self.loginInfo.userEmail.length > 0{
self.performSegueWithIdentifier("login_success", sender: self)
}
})
Here, I am checking whether response string has value or not.
Then call the method of segue to navigate to next page.
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!)
{
if segue.identifier == "login_success"
{
var mainVC: MainViewController!
mainVC = segue.destinationViewController as MainViewController
mainVC.mainLoginInfo = loginInfo
}
}
MainViewController is the page to which I want to move after login is done successfully.