Facebook login user data in Authentication doesn’t show in firebase consoles - swift

Every time, when I use a Facebook account to log in the application is supposed to have identified email shows up on the Firebase console but it doesn’t work properly. Users can use their Facebook account to access the application, but the problem is my profile page always got a crash when I attempt to make that page shows email of the users up. but if I use an email account to log in it doesn’t have any problem the email that I used to sign up able to shows up normally.
I have done everything in this link but can’t fix this problem.
https://firebase.google.com/docs/auth/ios/facebook-login
On the profile page, I use this code to call the email and user
import UIKit
import Firebase
import FirebaseAuth
import FacebookLogin
import FacebookCore
import FirebaseStorage
class ProfileViewController: UIViewController {
#IBOutlet weak var nameLabel: UILabel!
#IBOutlet weak var emailLabel: UILabel!
#IBOutlet weak var passTextField: UITextField!
#IBOutlet weak var changeNameText: UITextField!
#IBOutlet weak var menuButton: UIBarButtonItem!
#IBOutlet weak var imageProfile: UIImageView!
#IBOutlet weak var alertButton: UIBarButtonItem!
let imageUniqueName = UUID().uuidString
let imagePicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
let user = Auth.auth().currentUser
setUserDataToView(withFIRUser: user!)
customizeNavBar()
sideMenus()
let tapGesture = UITapGestureRecognizer()
tapGesture.addTarget(self, action: #selector(ProfileViewController.openGallery(tapGesture:)))
imageProfile.isUserInteractionEnabled = true
imageProfile.addGestureRecognizer(tapGesture)
imageProfile.drawAsCircle()
}
func setUserDataToView(withFIRUser user: User) {
nameLabel.text = user.displayName
emailLabel.text = "อีเมล์ : \(user.email!)"
}
this is all code on my LoginViewController page
import UIKit
import Firebase
import FirebaseAuth
import FBSDKCoreKit
import FBSDKLoginKit
import FBSDKCoreKit
import FacebookLogin
import FacebookCore
class LoginViewController: UIViewController, FBSDKLoginButtonDelegate {
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
if error != nil {
print("โปรดตรวจสอบใหม่อีกรอบ", error.localizedDescription)
} else if result.isCancelled {
} else {
let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
Auth.auth().signInAndRetrieveData(with: credential) { (authResult, error) in
ProgressHUD.showSuccess("ยินดีต้อนรับ")
self.performSegue(withIdentifier: "Main", sender: self)
}
}
}
func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
ProgressHUD.showSuccess("ออกจากระบบสำเร็จ")
}
let loginButton = FBSDKLoginButton()
//Textfields pre-linked with IBOutlets
#IBOutlet var emailTextfield: UITextField!
#IBOutlet var passwordTextfield: UITextField!
#IBOutlet weak var facebookButton: FBSDKLoginButton!
override func viewDidLoad() {
super.viewDidLoad()
loginButton.delegate = self
loginButton.readPermissions = ["public_profile", "email"]
self.navigationController?.setNavigationBarHidden(true, animated: false)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func logInPressed(_ sender: AnyObject) {
//TODO: Log in the user
Auth.auth().signIn(withEmail: emailTextfield.text!, password: passwordTextfield.text!) { (user, error) in
if error != nil{
let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
print(error!)
}else{
ProgressHUD.showSuccess("ยินดีต้อนรับ")
self.performSegue(withIdentifier: "Main", sender: self)
}
}
}
#IBAction func onClickPassword(_ sender: Any) {
if self.passwordTextfield.isSecureTextEntry == true {
self.passwordTextfield.isSecureTextEntry = false
}
else {
self.passwordTextfield.isSecureTextEntry = true
}
}
override func viewDidAppear(_ animated: Bool){
super.viewDidAppear(animated)
if Auth.auth().currentUser != nil {
self.performSegue(withIdentifier: "Main", sender: nil)
}
}
}
enter image description here

Related

Force unwrapping nil optional for UIImageView when transitioning to view controller

I'm running into an error when transitioning to view controllers by overriding the built-in prepare() function in Swift. I have a UIImageView for backgrounds on my screens. Here is the code for two of the view controllers in question.
import UIKit
import FirebaseAuth
class HomeVC: UIViewController {
#IBOutlet weak var signOutButton: UIButton!
#IBOutlet weak var backgroundImageView: UIImageView!
#IBOutlet weak var friendsNavButton: UIButton!
#IBOutlet weak var homeNavButton: UIButton!
#IBOutlet weak var profileNavButton: UIButton!
#IBOutlet weak var bumpButton: UIButton!
#IBOutlet weak var welcomeLabel: UILabel!
#IBOutlet weak var doNotDisturbLabel: UILabel!
#IBOutlet weak var doNotDisturbButton: UIButton!
var userName = ""
var dndIsOn: Bool = false
#IBAction func dndToggled(_ sender: Any) {
dndIsOn = !dndIsOn
User.current.available = !dndIsOn
FirestoreService.db.collection(Constants.Firestore.Collections.users).document(User.current.uid).updateData([Constants.Firestore.Keys.available : !dndIsOn])
if dndIsOn {
print("DND is on!")
setupDNDUI()
} else if !dndIsOn {
print("DND is off!")
setupActiveUI()
}
}
#IBAction func signOutTapped(_ sender: Any) {
let firAuth = Auth.auth()
do {
try firAuth.signOut()
} catch let signOutError as NSError {
print ("Error signing out: %#", signOutError)
}
print("Successfully signed out")
}
#IBAction func bumpTapped(_ sender: Any) {
self.performSegue(withIdentifier: Constants.Segues.toCall, sender: self)
}
#IBAction func friendsNavTapped(_ sender: Any) {
self.performSegue(withIdentifier: Constants.Segues.toFriends, sender: self)
}
#IBAction func profileNavTapped(_ sender: Any) {
let nav = self.navigationController //grab an instance of the current navigationController
DispatchQueue.main.async { //make sure all UI updates are on the main thread.
nav?.view.layer.add(CATransition().segueFromLeft(), forKey: nil)
nav?.pushViewController(ProfileVC(), animated: false)
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.setNavigationBarHidden(true, animated: true)
self.backgroundImageView.contentMode = UIView.ContentMode.scaleAspectFill
doNotDisturbLabel.isHidden = true
if !userName.isEmpty {
welcomeLabel.text = "Welcome Back, " + userName + "!"
} else {
welcomeLabel.text = ""
}
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .darkContent
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let friendsVC = segue.destination as? FriendsVC else {
return
}
FirestoreService.db.collection(Constants.Firestore.Collections.users).document(User.current.uid).getDocument { (snapshot, err) in
if let err = err {
print(err.localizedDescription)
} else {
let data = snapshot!.data()!
let requests = data[Constants.Firestore.Keys.requests] as? [String]
if let requests = requests {
friendsVC.requests = requests
}
}
}
}
class FriendsVC: UIViewController {
//var friends: [Friend] = User.current.friends
var friends: [User] = []
var requests: [String]?
#IBOutlet weak var requestsNumberLabel: UILabel!
#IBOutlet weak var backgroundImageView: UIImageView!
#IBOutlet weak var friendRequestsButton: UIButton!
#IBOutlet weak var homeNavButton: UIButton!
#IBOutlet weak var friendsTitle: UILabel!
#IBOutlet weak var friendTableView: UITableView!
#IBOutlet weak var addFriendButton: UIButton!
#IBOutlet weak var tableViewTopConstraint: NSLayoutConstraint!
#IBAction func friendRequestsTapped(_ sender: Any) {
self.performSegue(withIdentifier: Constants.Segues.toRequests, sender: self)
}
#IBAction func homeNavTapped(_ sender: Any) {
let nav = self.navigationController //grab an instance of the current navigationController
DispatchQueue.main.async { //make sure all UI updates are on the main thread.
nav?.view.layer.add(CATransition().segueFromLeft(), forKey: nil)
nav?.pushViewController(HomeVC(), animated: false)
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.setNavigationBarHidden(true, animated: true)
backgroundImageView.contentMode = UIView.ContentMode.scaleAspectFill
friendTableView.backgroundView?.backgroundColor = .white
friendsTitle.isHidden = false
UserService.getUserArray(uids: User.current.friendUids, completion: { (users) in
guard let users = users else {
print("User has no friends")
return
}
self.friends = users
self.friendTableView.reloadData()
})
guard let requests = self.requests else {
friendRequestsButton.isHidden = true
requestsNumberLabel.isHidden = true
self.tableViewTopConstraint.constant = 0
return
}
requestsNumberLabel.text = requests.count.description
// Do any additional setup after loading the view.
friendTableView.delegate = self
friendTableView.dataSource = self
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let homeVC = segue.destination as? HomeVC {
homeVC.userName = User.current.firstName
} else if let requestsVC = segue.destination as? RequestsVC {
UserService.getUserArray(uids: self.requests!) { (requesters) in
if let requesters = requesters {
requestsVC.requesters = requesters
}
}
}
}
}
When my app loads into the home screen, there is no problem, and when a button is tapped to transition to FriendsVC, there is no problem. However, when I try to initiate the transition from HomeVC to ProfileVC or from FriendVC to HomeVC, I get the error: "Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value" at the self.backgroundImageView.contentMode = UIView.ContentMode.scaleAspectFill lines in my viewDidLoad methods. These segues have something in common in that these are the ones where I override the prepare() function, but I'm not sure what I'm doing wrong

Swift 4 how to print text to label

I am trying to print text into a text field like this:
How to do that the right way?
else {
weak var worgnlogin: UILabel! {
worgnlogin.text = ("brugernavn eller password er skrevet forkert")
}
}
Here you have my main ViewController code I have made an array with all users infomation, as I will loop through and get the right user to login:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var txtUserName: UITextField!
#IBOutlet weak var txtPassword: UITextField!
#IBOutlet weak var worgLogin: UILabel!
let user1 = ["user": "Karsten","userID":"1","userName":"Kalle","passWord":"1234" ]
let user2 = ["user": "Rene","userID":"2","userName":"Rene" ,"passWord":"1234" ]
let user3 = ["user": "Johan","userID":"3","userName":"Johan","passWord":"1234" ]
override func viewDidLoad() {
super.viewDidLoad()
let array = [user1,user2,user3]
UserDefaults.standard.set(array, forKey: "users")
// Do any additional setup after loading the view, typically from a nib.
if UserDefaults.standard.bool(forKey: "ISUSERLOGGEDIN") == true {
//user is already logged in just navigate him to home screen
let homeVc = self.storyboard?.instantiateViewController(withIdentifier: "HomeVC") as! HomeVC
self.navigationController?.pushViewController(homeVc, animated: false)
}
}
#IBAction func authenticateUser(_ sender: Any) {
if txtUserName.text == "userName" && txtPassword.text == "passWord" {
//navigate to home screen
UserDefaults.standard.set(true, forKey: "ISUSERLOGGEDIN")
let homeVc = self.storyboard?.instantiateViewController(withIdentifier: "HomeVC") as! HomeVC
self.navigationController?.pushViewController(homeVc, animated: true)
}else {
displayMyAlertMessage(userMessage: "Brugernavn eller Password er skrevet forkert");
return;
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func displayMyAlertMessage(userMessage:String)
{
let myAlert = UIAlertController(title:"Alert", message:userMessage, preferredStyle: UIAlertController.Style.alert);
let okAction = UIAlertAction(title:"Ok", style:UIAlertAction.Style.default, handler:nil);
myAlert.addAction(okAction);
self.present(myAlert, animated:true, completion:nil);
}
}
i hobe this make a better understanding :)
worgnlogin.text = "brugernavn eller password er skrevet forkert"
First create an IBOutlet for a label:
#IBOutlet weak var worgnLoginLabel: UILabel!
Then in your else statement write:
self.worgnLoginLabel.text = “your message”

Swift Error: EXC BAD INSTRUCTION code=EXCI386 for userid

I am writing an app login page when I countered this error. I do not know how to debug it so I am asking where I coded wrong. I got a problem on the keychain wrapper code:
KeychainWrapper.standard.set((user?.uid)!,forKey: "uid")
I am following a YouTube tutorial on Swift 4, the most updated version. However, it seems this line of code has some problems, therefore I would like to know the problem in the whole code, whether I missed I bracket or failed to import.
import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
import FirebaseCore
import SwiftKeychainWrapper
import FirebaseStorage
class ViewController: UIViewController {
#IBOutlet weak var usernameField: UITextField!
#IBOutlet weak var emailField: UITextField!
#IBOutlet weak var passwordField: UITextField!
#IBOutlet weak var userimageview: UIImageView!
var imagePicker: UIImagePickerController!
var selectedImage = UIImage()
override func viewDidLoad() {
super.viewDidLoad()
imagePicker = UIImagePickerController()
imagePicker.allowsEditing = true
imagePicker.delegate = self
}
override func viewDidAppear(_ animated: Bool) {
if KeychainWrapper.standard.object(forKey: "KEY_UID") != nil {
self.performSegue(withIdentifier: "toFeed", sender: nil)
}
}
func storeuserdata(userId: String) {
if let imageData = UIImageJPEGRepresentation(selectedImage, 0.2) {
Storage.storage().reference().putData(imageData, metadata: nil) { (metadata, error) in
guard let metadata = metadata else {
// Uh-oh, an error occurred!
return
}
// Metadata contains file metadata such as size, content-type, and download URL.
let downloadURL = metadata.downloadURL
Database.database().reference().child("users").child(userId).setValue(["username": self.usernameField.text!,"userImg": downloadURL
])
}
}
}
#IBAction func signinpressed(_ sender: Any) {
if let email = emailField.text, let password = passwordField.text {
Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
if error != nil && (self.usernameField.text?.isEmpty)! && self.userimageview.image != nil {
Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
self.storeuserdata(userId: (user?.uid)!)
KeychainWrapper.standard.set((user?.uid)!,forKey: "uid")
self.performSegue(withIdentifier: "toFeed", sender: nil)
}
}else {
KeychainWrapper.standard.set((user?.uid)!,forKey: "uid")
self.performSegue(withIdentifier: "toFeed", sender: nil)
}
}
}
}
#IBAction func getPhoto (_ sender: AnyObject) {
present(imagePicker, animated: true, completion: nil)
}
}
extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate{
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
selectedImage = image
}
imagePicker.dismiss(animated: true, completion: nil)
}
}

Writing User Data to Firebase After Creating New User Account

I am currently trying to write the users information to firebase's database after using the create user function FIRAuth.auth()?.createUser
Under this function I attempt to insert the data into the database like this:
FIRAuth.auth()?.createUser(withEmail: self.emailField.text!, password: self.passwordField.text!) { (user, error) in
if error == nil
{
let email = self.emailField.text
let firstName = self.firstnameField.text
let lastName = self.lastnameField.text
self.ref.child((user?.uid)!).setValue(["firstName": firstName,"lastName": lastName,"email": email])
self.performSegue(withIdentifier: "createaccountLandingPage", sender: sender)
}
It also may be important to mention that under my view controller I create the reference to the database using:
var ref = FIRDatabase.database().reference().child("users") //root database
My outlets are all correct, but I am getting this error:
Thread 1: signal SIGABRT
Any suggestions on what I may be doing incorrectly?
EDIT** Here is all my code in the signup view controller
import UIKit
import Firebase
import FirebaseDatabase
class CreateAccountViewController: UIViewController {
var ref = FIRDatabase.database().reference().child("users") //root database
#IBOutlet weak var firstnameField: UITextField!
#IBOutlet weak var lastnameField: UITextField!
#IBOutlet weak var emailField: UITextField!
#IBOutlet weak var passwordField: UITextField!
#IBOutlet weak var confirmpasswordField: UITextField!
#IBOutlet weak var createAccountButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
if (FIRAuth.auth()?.currentUser) != nil
{
}
else
{
}
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func createAccountAction(_ sender: AnyObject)
{
if self.confirmpasswordField.text != self.passwordField.text
{
}
else
{
FIRAuth.auth()?.createUser(withEmail: self.emailField.text!, password: self.passwordField.text!) { (user, error) in
if error == nil
{
let user = FIRAuth.auth()?.currentUser.uid
let email = self.emailField.text
let firstName = self.firstnameField.text
let lastName = self.lastnameField.text
self.ref.child("users").child("\(user)").setValue(["firstName": firstName,"lastName": lastName,"email": email])
self.performSegue(withIdentifier: "createaccountLandingPage", sender: sender)
}
else
{
let alertController = UIAlertController(title: "Oops!", message: error?.localizedDescription, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
}
}
}
EDIT*** here is a screenshot of the error
EDIT**** here is a screenshot of my podfile and when it is updated I recieve no errors in the terminal.
EDIT***** Screenshots of console.
1stScreenshotConsole
2ndScreenshotConsole
You have to create a reference to your database like this
Var ref: FIRDatabaseReference!
Then you initialize your database in your viewDidLoad like this
Ref = FIRDatabase.database().reference()
I wrote the whole code below, excuse the syntax errors because I am answering your question on an iPad.
import UIKit
import Firebase
import FirebaseDatabase
class CreateAccountViewController: UIViewController {
var ref = FIRDatabaseReference! //create a reference for your database
#IBOutlet weak var firstnameField: UITextField!
#IBOutlet weak var lastnameField: UITextField!
#IBOutlet weak var emailField: UITextField!
#IBOutlet weak var passwordField: UITextField!
#IBOutlet weak var confirmpasswordField: UITextField!
#IBOutlet weak var createAccountButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
//initialize your database
ref = FIRDatabase.database().reference()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func createAccountAction(_ sender: AnyObject)
{
if self.confirmpasswordField.text != self.passwordField.text
{
}
else
{
FIRAuth.auth()?.createUser(withEmail: self.emailField.text!, password: self.passwordField.text!) { (user, error) in
if error == nil
{
let user = FIRAuth.auth()?.currentUser.uid //get the users UID after registering
let email = self.emailField.text
let firstName = self.firstnameField.text
let lastName = self.lastnameField.text
self.ref.child("users").child("\(user!)").setValue(["firstName": "\(firstName!)", "lastName": "\(lastName!)", "email": "\(email!)"])
self.performSegue(withIdentifier: "createaccountLandingPage", sender: sender)
}
else
{
let alertController = UIAlertController(title: "Oops!", message: error?.localizedDescription, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
}
}
}

How to call segue/ unwind right after login with Facebook

I created a log-in screen with 3 text fields (username, email, password). with 3 action buttons (Register, Login, Facebook login).
I was able to log in with facebook account, but I can't seem to present the details of the user, such name, and profile pic.. I tried to displayed it on the currentviewcontroller or the destinationVC but I guess I'm doing something wrong with the segue and Facebook
import UIKit
import Parse
import FBSDKCoreKit
import ParseFacebookUtilsV4
var myAlert = ""
var user = PFObject(className:"User")
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet var userName: UITextField!
#IBOutlet var email: UITextField!
#IBOutlet var passWord: UITextField!
#IBOutlet weak var userlab: UILabel!
let permissions = ["public_profile", "user_friends", "email"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.userName.delegate = self
self.email.delegate = self
self.passWord.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func LogInToFacebook(sender: AnyObject) {
PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions) {
(user: PFUser?, error: NSError?) -> Void in
if let user = user {
if user.isNew {
print("User signed up and logged in through Facebook!")
self.userlab.text = PFUser.currentUser()?.username
self.performSegueWithIdentifier("CSegue", sender: self)
} else {
print("User logged in through Facebook!")
//self.image = UIImage(named: "logout_FB.png")!
self.userlab.text = PFUser.currentUser()?.username
//self.fbOutlet.setImage(self.image, forState: UIControlState.Normal)
self.performSegueWithIdentifier("CSegue", sender: self)
}
} else {
print("Uh oh. The user cancelled the Facebook login.")
}
}
}
at the destinationVC (HomeVC) I just created 3 labels
import UIKit
import Parse
import FBSDKCoreKit
import ParseFacebookUtilsV4
class HomeVC: UIViewController {
#IBOutlet weak var userNamelabel: UILabel!
var user: PFUser!
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "LogInToFacebook" {
user = PFUser.currentUser()
userNamelabel.text = user.username
}
}
That's the segue I am trying to use:
override func perform() {
let sourceViewController: UIViewController = self.sourceViewController
let destinationViewController: UIViewController = self.destinationViewController
user = PFUser.currentUser()!
sourceViewController.view.addSubview(destinationViewController.view)
destinationViewController.view.transform = CGAffineTransformMakeScale(0.05, 0.05)
UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
destinationViewController.view.transform = CGAffineTransformMakeScale(1.0, 1.0)
}) { (finished) -> Void in
destinationViewController.view.removeFromSuperview()
sourceViewController.presentViewController(destinationViewController, animated: false, completion: nil)
}
}