Facebook LoginManager Callback/PrepareforSegue Swift - ios

I'm trying to pass a property to my next VC using prepareforSegue. It is called, however my other VC does not load.
Original
#IBAction func onSignupPressed(sender: AnyObject) {
FBSDKLoginManager().logInWithReadPermissions(permissions,
fromViewController: self) { result, error in
guard error == nil else { print("Login Error"); return }
guard result.grantedPermissions.contains("email") else {
print("No Email Permissions"); return }
self.getFBUserData()
dispatch_async(dispatch_get_main_queue()) {
self.performSegueWithIdentifier("loginSegue", sender: self)
}
}
}
I've been able to get the next VC showing by doing calling my doLogin function below. And there I instantiate a VC from the storyboard and present it. I believe it is due to the timing of the Facebook login window that pops up and closes. I searched for a delegate method, but have not found anything
#IBAction func onSignupPressed(sender: AnyObject) {
FBSDKLoginManager().logInWithReadPermissions(permissions,
fromViewController: self) { result, error in
guard error == nil else { print("Login Error"); return }
guard result.grantedPermissions.contains("email") else {
print("No Email Permissions"); return }
self.getFBUserData()
dispatch_async(dispatch_get_main_queue()) {
self.performSegueWithIdentifier("loginSegue", sender: self)
}
}
}
func getFBUserData(){
let params = "id, name, first_name, last_name, picture.type(large),friends, email, birthday, work, photos, education, location, hometown, religion, likes, about"
if((FBSDKAccessToken.currentAccessToken()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields":params]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
//everything works print the user data
print(result)
let resultdict = result as? NSDictionary
self.user = Mapper<User>().map(result)
self.doLogin()
}
})
}
}
func doLogin() {
let successVC = self.storyboard?.instantiateViewControllerWithIdentifier("LoginSucessViewController")
self.presentViewController(successVC!, animated: true, completion: nil)
}

Very silly mistake! Didn't realize I could simply set the property of instantiating the VC!
func doLogin() {
let successVC = self.storyboard?.instantiateViewControllerWithIdentifier("LoginSucessViewController") as! LoginSucessViewController
successVC.currentUser = user
self.presentViewController(successVC, animated: true, completion: nil)
}

Related

Only instance methods can be declared #IBAction error?

I am facing this error on build for the function shown in the code
Only instance methods can be declared #IBAction
this error is coming up only after I introduced google sign in method for similar functionality , earlier it not an error
#IBAction func SignInButtonAction(_ sender: Any) {
guard let email = emailField.text else { return }
guard let pass = passwordField.text else { return }
Auth.auth().signIn(withEmail: email, password: pass) { user, error in
if error == nil && user != nil {
let setupcheckref = Firestore.firestore().collection("users").document(Auth.auth().currentUser!.uid)
setupcheckref.getDocument{(document, error) in
if let document = document, document.exists{
let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
self.checksetup = document.get("setupComplete") as! Bool
if self.checksetup == true {
if Auth.auth().currentUser!.isEmailVerified {
self.performSegue(withIdentifier: "toLoginFeed", sender: self)
}
else{
print("please verify your email")
try! Auth.auth().signOut()
}
}
else{
self.view.makeToast("Please Setup Your Account!", duration: 2.5)
self.performSegue(withIdentifier: "fromlogintosetup", sender: self)
SVProgressHUD.dismiss()
} }
}
// self.dismiss(animated: false, completion: nil)
} else {
print("Error logging in: \(error!.localizedDescription)")
// self.resetForm()
// SVProgressHUD.dismiss()
}
}
}
That means you can create #IBActions only as instance methods of a class.
You might be creating it of a class.
class VC: UIViewController {
#IBAction func SignInButtonAction(_ sender: Any) {
//your code...
}
}

rootViewController not loading after calling it, using -> present(viewControllerToPresent: UIViewController, animated: true, completion: nil)

My rootViewController works and loads fine. However, when I call it using the present(viewControllerToPresent: UIViewController, animated: true, completion: nil) from another viewController I get nothing but a black screen.
I looked all over the stackOverflow but only found solutions for storyboard users. I am doing this programmatically
#objc func handleLogin() {
print("LOGIN BUTTON TOUCHED")
guard let email = emailTextField.text, let password = passwordTextField.text else {
print("Form is not valid.")
return
}
Auth.auth().signIn(withEmail: email, password: password) { (user, error) in
if error != nil {
print(error!)
return
}
let viewController = RootViewController()
*** self.present(viewController, animated: true, completion: nil)
print("Logged in")
}
}
There is nothing wrong with self.present(viewController, animated: true, completion: nil)
I think your RootViewController() is already presented. The black screen you see is might be the one without any data? I am not sure, will need your code for that class.
Another approach you can consider is to replace the real rootViewController from current uiWindow like this
#objc func handleLogin() {
print("LOGIN BUTTON TOUCHED")
guard let email = emailTextField.text, let password = passwordTextField.text else {
print("Form is not valid.")
return
}
Auth.auth().signIn(withEmail: email, password: password) { [weak self] (user, error) in
guard let strongSelf = self else { return }
guard error == nil else { return }
guard let user = user else { return }
UIApplication.shared.keyWindow?.rootViewController = RootViewController()
print("Logged in")
}
}
you've created a new instance(RootViewController) and it must be black .. your not referring to your RootViewController

Firebase Anonymous Auth, Nil User

I'm using Firebase database and offer anonymous login. The first anonymous login made on a single device works as expected. If I sign out and attempt any more anonymous logins, it succeeds, the completion block has no error and returns a user.
However, once it's all done and we're out of the completion block, Auth.auth().currentUser() is nil.
If I run a simple Timer checking Auth.auth().currentUser() every second, throughout the entire login process it is always nil and never changes.
Quick breakdown of code:
Login anonymously.
Check if id exists in db.
Update profile displayName with id for easy referral later.
Fetch client in db.
All go wrong!
Tap a button to sign in.
#IBAction func clientLoginBtnTap(_ sender : UIButton) {
self.clientActivityIndicator?.showActivityIndicator()
Auth.auth().signInAnonymously { (user, error) in
if error == nil {
//check id matches available client
self.checkClient(id: (self.clientIdField?.text)!, completion: { (isValid) in
if isValid == true {
//now signed in, update client id
let profileChangeRequest = user?.createProfileChangeRequest()
profileChangeRequest?.displayName = self.clientIdField?.text
profileChangeRequest?.commitChanges(completion: { (error) in
if error == nil {
//done
UserDefaults.standard.set(true, forKey: kIS_USER_CLIENT_NOT_TRAINER)
self.dismiss(animated: true, completion: {
//self.delegate?.didLoginAsClient()
})
}
else {
self.logout()
self.clientIdField?.shake()
self.clientActivityIndicator?.hideActivityIndicator()
}
})
}
else {
self.logout()
self.clientIdField?.shake()
self.clientActivityIndicator?.hideActivityIndicator()
}
})
}
else {
}
}
}
func checkClient(id : String, completion: #escaping (_ isValid : Bool) -> Void) {
let ref = Database.database().reference().child("v2").child("clients").child(id)
ref.observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists() { completion(true) }
else { completion(false) }
}) { (error) in
completion(false)
}
}
func logout() {
do {
try Auth.auth().signOut()
}
catch let error as NSError {
print (error.localizedDescription)
}
}
Login is successful.
Then this runs after login and the user exists but Auth.auth().currentUser() is nil. When a client login happens, I try to get the client data but permission is denied because we have no user.
handle = Auth.auth().addStateDidChangeListener { (auth, user) in
self.currentUser = user
if user == nil {
self.updateForNoUser()
}
else {
self.updateForUser()
}
}
func updateForUser() {
//Trainer Logged in
if UserDefaults.standard.bool(forKey: kIS_USER_CLIENT_NOT_TRAINER) == false {
self.performSegue(withIdentifier: "master", sender: self)
}
//Client Logged in
else {
if let id = Auth.auth().currentUser?.displayName {
let ref = Database.database().reference().child("v2").child("clients").child(id)
ref.observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists() {
self.client = Client(snapshot: snapshot)
self.performSegue(withIdentifier: "masterClient", sender: self)
}
}) { (error) in }
}
else {
do {
try Auth.auth().signOut()
}
catch let error as NSError {
print (error.localizedDescription)
}
}
}
}

The method return the varible without modifying it

i have a problem with my code in Swift
the method verifyInput should return false when there's an error
but it always return true no matter what + when there's an error it print "error" but its just return true
please help
#IBAction func register(_ sender: UIButton) {
let check = verifyInput(email :email.text! ,password: password.text!)
if(check==true){
self.performSegue(withIdentifier: "goToAmazon", sender: nil)
} else if(check==false) {
self.message.text = "Sorry! there's an error"
}
}
func verifyInput(email: String, password: String) -> Bool {
var check = true
Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
if error != nil {
print("error")
check = false
} else if(error==nil){
check = true
print("registered!")
}
}
return check
}
The problem is that verifyInput is being called synchronously from register but within it is an asynchronous call to Auth.auth().createUser with a completion block.
The check result is being returned before the asynchronous call ever completes. You need to change your method to be asynchronous as well.
Something vaguely like this is what you want:
#IBAction func register(_ sender: UIButton) {
if let email = email.text, let password = password.text {
verifyInput(email: email, password: password) { (check) in
DispatchQueue.main.async {
// only run UI code on the main thread
if(check){
self.performSegue(withIdentifier: "goToAmazon", sender: nil)
} else {
self.message.text = "Sorry! there's an error"
}
}
}
}
}
func verifyInput(email: String, password: String, escaping completion:#escaping (Bool)->Void) {
Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
if error != nil {
print("error")
completion(false)
} else if(error==nil){
print("registered!")
completion(true)
}
}
}

Facebook Login Swift

I wrote a basic Swift Class for Facebook login handling.
I want to check if the user already authorised the App, because in my case, the user gets asked everytime if he authorises the app - it switches to safari everytime instead of simply logging in. Sometimes the login completely fails - no error message, but also no success.
Here is my code:
class FacebookLogin{
private var data : NSMutableData? = nil
init(){
}
let facebookReadPermissions = ["public_profile", "email", "user_friends"]
func loginToFacebookWithSuccess(successBlock: () -> (), andFailure failureBlock: (NSError?) -> ()) {
if FBSDKAccessToken.currentAccessToken() != nil {
return
}
FBSDKLoginManager().logInWithReadPermissions(self.facebookReadPermissions, handler: { (result:FBSDKLoginManagerLoginResult!, error:NSError!) -> Void in
if error != nil {
// Process error
FBSDKLoginManager().logOut()
failureBlock(error)
} else if result.isCancelled {
// Handle cancellations
FBSDKLoginManager().logOut()
failureBlock(nil)
} else {
// If you ask for multiple permissions at once, you
// should check if specific permissions missing
var allPermsGranted = true
let grantedPermissions = Array(result.grantedPermissions).map( {"\($0)"} )
for permission in self.facebookReadPermissions {
if !contains(grantedPermissions, permission) {
allPermsGranted = false
break
}
}
if allPermsGranted {
// Do work
let fbToken = result.token.tokenString
let fbUserID = result.token.userID
println(fbUserID)
successBlock()
} else {
//The user did not grant all permissions requested
failureBlock(nil)
}
}
})
}
func login() -> Void{
if(self.alreadyLoggedIn() == false){
self.performLogin()
}else{
self.getFBUserData()
}
}
private func performLogin() -> Void {
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager();
fbLoginManager.loginBehavior = FBSDKLoginBehavior.SystemAccount
println(FBSDKAccessToken.currentAccessToken())
fbLoginManager.logInWithReadPermissions(["email"], handler: { (result, error) -> Void in
if (error == nil){
var fbloginresult : FBSDKLoginManagerLoginResult = result
if(fbloginresult.grantedPermissions.contains("email"))
{
self.getFBUserData()
fbLoginManager.logOut()
}
}
})
}
private func alreadyLoggedIn() -> Bool {
if (FBSDKAccessToken.currentAccessToken() != nil)
{
return true
}else{
return false
}
}
private func getFBUserData() -> Void{
if((FBSDKAccessToken.currentAccessToken()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
println(result)
}
})
}
}
}
Thanks
In your view controller viewDidLoad() you can add this code :
if (FBSDKAccessToken.currentAccessToken() == nil)
{
// User is not already logged
println("No Logged")
}
else
{
// User is already logged
println("Already Logged")
}

Resources