Local Authentication not changing view controllers - ios

I'm having an issue with changing view controllers in local authentication. When all the code executes in the success if statement the view controller does not change even though I'm telling it to. I've tried everything that I know but nothing works. Here is my local authentication code.
let authentication = LAContext()
var authenticationError: NSError?
authentication.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &authenticationError)
if (authenticationError != nil) {
// Authentication Not available for this version of iOS
self.gotoMainViewController()
} else {
authentication.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: "Access Passy using Touch ID") {
(success, error) in
if (error != nil) {
// There was an error - user likley pressed cancel
print(error?.localizedDescription)
} else {
if (success) {
dispatch_async(dispatch_get_main_queue()) {
self.gotoMainViewController()
}
} else {
self.showFailedTouchIDError.showAlert()
}
}
}
}
Here is the gotoMainViewController() code.
func gotoMainViewController() {
let viewController = MainViewController()
self.navigationController?.pushViewController(viewController, animated: true)
}

I figured it out!! It seems that local authentication needs a boolean IF statement to be wrapped around all of the code. I'm not sure if this is true...but it worked for me.

Related

Swift application having weird behavior when closing it

I am developing an application that uses the Face/Touch ID at the opening.
I achieved this by adding this func to my MainViewController():
let context = LAContext()
if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: nil) {
context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: "Verifying") { (success, err) in
if success {
DispatchQueue.main.async {
self.loginSuccessfull()
self.button.removeFromSuperview()
}
} else {
if let err = err {
print(err)
}
}
}
}
This gets called both in the ViewDidLoad and by a button, as shown in this video.
As you can see tho, when I try to close my App it has a very weird behavior, and I am sure that it is caused by the FaceID.
Any suggestion to fix this?
Crash log:
Error Domain=com.apple.LocalAuthentication Code=-4 "Caller moved to background." UserInfo={NSLocalizedDescription=Caller moved to background.}
I believe I have found a solution for the issue, by delaying the evaluation.
I noticed that when I have any kind of delay in UI before evaluation (for example: animation that move the logo up before showing the face ID alert) the crash stops altogether.
So I did another test with delay like so:
override func viewDidAppear(_ animated: Bool) {
let context = LAContext()
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Biometric test") { success, error in
DispatchQueue.main.async {
if success {
doSome()
} else {
if let error = error { print(error) }
}
}
}
}
}
}
With that implementation I had zero crashes.
*Note: I also tried different delay times, from 0.1 to 2.0 seconds, all worked for me.

App crashes after deleting user

So in my app I just made it so that the user can choose to delete their account, and that works out beautifully. However after the account is deleted the app crashes. I think this is because it is trying to search for a user but it's not there.
Here is my code:
let loginController = LoginController()
func deleteAccount() {
let user = Auth.auth().currentUser
let userId = Auth.auth().currentUser?.uid
let databaseUser = Database.database().reference().child("users").child(userId!)
user?.delete { error in
if let error = error {
print(error)
} else {
self.present(self.loginController, animated: true, completion: nil)
}
}
databaseUser.removeValue(completionBlock: { (error, ref) in
if error != nil {
print(error)
} else {
self.present(self.loginController, animated: true, completion: nil)
} //Without doing this the user's account only gets deleted in the Authentication, not the whole database. I think this is the problem here?
})
}
Thank you so much in advance!
First you need to delete the users database as to do that you would need the let userId = Auth.auth().currentUser?.uid which is only active if the user itself is on your backend, Then you go on to delete the auth.
func deleteAccount() {
let user = Auth.auth().currentUser
let userId = Auth.auth().currentUser!.uid
let databaseUser = Database.database().reference().child("users").child(userId)
databaseUser.removeValue(completionBlock: { (error, ref) in
if error != nil {
print(error)
} else {
user?.delete { error in
if let error = error {
print(error)
} else {
self.present(self.loginController, animated: true, completion: nil)
}
}
})
}
If this still doesn't work track down the lifecycle of the user using debugging tools....
You are implicitly unwrapping an optional in this line with !:
let databaseUser = Database.database().reference().child("users").child(userId!)
You should check whether it's nil in the first place with a guard statement:
guard let userId = Auth.auth().currentUser?.uid else {
return
}
let databaseUser = Database.database().reference().child("users").child(userId)
[...]
Moreover, your code logic is likely to be wrong, as you are getting nil for the userId before you can work with it.
The code order is not ideal because deleting the user also logs them out. So the code may be trying to access the users node after the user was logged out.
Also remember that Firebase is asynchronous and the only way to know a function has completed is when the code inside the closure executes i.e. in this case the databaseUser.removeValue may be firing before the delete user or sometimes it may not.
Code is faster than the internet so it's best to leverage the closures so you know when it's safe to proceed.
Try this sequence; noting that we don't try to delete the Firebase user until we know for sure the data in the users node was deleted. There could use more error checking but you get the idea.
let userRef = self.ref.child("users").child(uid)
userRef.setValue(nil, withCompletionBlock: { snapshot in
Auth.auth().currentUser?.delete(completion: { err in
if err != nil {
print(err?.localizedDescription)
}
})
})

Can't get login call back with Facebook SDK swift

I'm trying to login to facebook with facebook sdk but when user authorized the app, it shows blank screen like this and can't get callback.
It used to work fine, but suddenly, it doesn't work.
let manager = FBSDKLoginManager()
manager.logInWithReadPermissions(["public_profile", "email", "user_friends"], fromViewController: self) { (result, error) in
if error != nil {
print(error.localizedDescription)
}
else if result.isCancelled {
print("Facebook login cancelled")
}
else {
let token = FBSDKAccessToken.currentAccessToken().tokenString
let credential = FIRFacebookAuthProvider.credentialWithAccessToken(token)
FIRAuth.auth()?.signInWithCredential(credential, completion: { (user, error) in
if error != nil {
print(error?.localizedDescription)
}
else {
self.performSegueWithIdentifier("MainScreenSignUp", sender: self)
}
})
}
}
Is there anybody who has experience in this area?
I was stuck by the same, it is due to the property "loginBehavior" of the "FBSDKLoginManager", i get it working using: manager.loginBehavior = .Web
By default it uses the ".Native" that gets the result you posted.
Hope it helps!
Excuse my english!

How to check if user has valid Auth Session Firebase iOS?

I wanna check if the user has still a valid session, before I present the Home View controller of my app. I use the latest Firebase API. I think if I use the legacy, I'll be able to know this.
Here's what I did so far:
I posted my question on Slack community of Firebase, no one is answering. I found this one, but this is for Android: https://groups.google.com/forum/?hl=el#!topic/firebase-talk/4HdhDvVRqHc
I tried reading the docs of Firebase for iOS, but I can't seem to comprehend it: https://firebase.google.com/docs/reference/ios/firebaseauth/interface_f_i_r_auth
I tried typing in Xcode like this:
FIRApp().currentUser()
FIRUser().getCurrentUser()
But I can't seem to find that getCurrentUser function.
if FIRAuth.auth().currentUser != nil {
presentHome()
} else {
//User Not logged in
}
For updated SDK
if Auth.auth().currentUser != nil {
}
Updated answer
Solution for latest Firebase SDK - DOCS
// save a ref to the handler
private var authListener: AuthStateDidChangeListenerHandle?
// Check for auth status some where
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
authListener = Auth.auth().addStateDidChangeListener { (auth, user) in
if let user = user {
// User is signed in
// let the user in?
if user.isEmailVerified {
// Optional - check if the user verified their email too
// let the user in?
}
} else {
// No user
}
}
}
// Remove the listener once it's no longer needed
deinit {
if let listener = authListener {
Auth.auth().removeStateDidChangeListener(authListener)
}
}
Original solution
Solution in Swift 3
override func viewDidLoad() {
super.viewDidLoad()
FIRAuth.auth()!.addStateDidChangeListener() { auth, user in
if user != nil {
self.switchStoryboard()
}
}
}
Where switchStoryboard() is
func switchStoryboard() {
let storyboard = UIStoryboard(name: "NameOfStoryboard", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "ViewControllerName") as UIViewController
self.present(controller, animated: true, completion: nil)
}
Source
Solution in Swift 4
override func viewDidLoad() {
super.viewDidLoad()
setupLoadingControllerUI()
checkIfUserIsSignedIn()
}
private func checkIfUserIsSignedIn() {
Auth.auth().addStateDidChangeListener { (auth, user) in
if user != nil {
// user is signed in
// go to feature controller
} else {
// user is not signed in
// go to login controller
}
}
}
if Auth.auth().currentUser?.uid != nil {
//user is logged in
}else{
//user is not logged in
}
While you can see if there is such a user using Auth.auth().currentUser, this will only be telling you if there was a user authenticated, regardless of whether that users account still exists or is valid.
Complete Solution
The real solution to this should be using Firebase's re-authentication:
open func reauthenticate(with credential: AuthCredential, completion: UserProfileChangeCallback? = nil)
This assures (upon the launch of the application) that the previously signed in / authenticated user still in fact is and can be authenticated through Firebase.
let user = Auth.auth().currentUser // Get the previously stored current user
var credential: AuthCredential
user?.reauthenticate(with: credential) { error in
if let error = error {
// An error happened.
} else {
// User re-authenticated.
}
}
override func viewDidLoad() {
FIRAuth.auth()!.addStateDidChangeListener() { auth, user in
// 2
if user != nil {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "Home")
self.present(vc!, animated: true, completion: nil)
}
}
}
Source: https://www.raywenderlich.com/139322/firebase-tutorial-getting-started-2
An objective-c solution would be (iOS 11.4):
[FIRAuth.auth addAuthStateDidChangeListener:^(FIRAuth * _Nonnull auth, FIRUser * _Nullable user) {
if (user != nil) {
// your logic
}
}];
All the provided answers only check on currentUser. But you could check the auth session by simple user reload like below:
// Run on the background thread since this is just a Firestore user reload, But you could also directly run on the main thread.
DispatchQueue.global(qos: .background).async {
Auth.auth().currentUser?.reload(completion: { error in
if error != nil {
DispatchQueue.main.async {
// Authentication Error
// Do the required work on the main thread if necessary
}
} else {
log.info("User authentication successfull!")
}
})
}

iOS Facebook Access Token not caching

I have a custom button that I use for Facebook login, and it was working fine until recently. The access token was cached and the next time the user launched the app, the continue button was displayed in its place.
Recently however the marked line returns nil regardless of whether the user has previously logged in. I'm at a loss as to why - I haven't made any code changes in this part of the app?
Occasionally the login will fail with the following error also:
Error Domain=com.facebook.sdk.login Code=308 "(null)"
Here's my code:
override func viewDidLoad() {
super.viewDidLoad()
if (FBSDKAccessToken.currentAccessToken() == nil){ // <<<< ALWAYS RETURNS NIL
self.continueButton.hidden = true
} else {
self.loginButton.hidden = true
self.notYouButton.hidden = false
}
}
#IBAction func loginPressed(sender: AnyObject) {
let permissions = ["user_about_me","user_relationships","user_birthday","user_location","user_status","user_posts", "user_photos"]
let login = FBSDKLoginManager()
login.logInWithReadPermissions(permissions, handler: {
(FBSDKLoginManagerLoginResult result, NSError error) -> Void in
if(error == nil){
self.loginButton.hidden = true
self.continueButton.hidden = false
self.notYouButton.hidden = false
self.notYouButton.enabled = false
//self.performSelector("showBrowse", withObject: nil, afterDelay: 1.0)
} else {
print(error)
}
})
}
EDIT: On further testing it seems that calling FBSDKAccessToken.currentAccessToken() is returning nil if called in viewDidLoad(), but if I call it from a button press it returns the Facebook token as expected.
override func viewDidLoad() {
super.viewDidLoad()
if let token = FBSDKAccessToken.currentAccessToken() {
print (token)
} else {
print ("no token") <<<<< RETURNS
}
}
#IBAction func buttonPressed(sender: AnyObject) {
if let token = FBSDKAccessToken.currentAccessToken() {
print (token) <<<<< RETURNS
} else {
print ("no token")
}
}
It turns out that there was a problem in my appDelegate where I was setting up a custom View Controller. I reverted the code to use storyboards and the issue was resolved - not a resolution per se for anyone with similar issues but it's enough for me to get on.

Resources