Search code examples
iosswiftswiftyuserdefaults

Sign up button clicked function


user.signUpInBackground{ (success: Bool, error:Error?) in
    if success{
        print("signed up")
        UserDefaults.standard.set(user.username, forKey: "username")
        UserDefaults.standard.synchronize()
        user.signUpInBackground {(success: Bool, error: Error?) in
            if success {
                print("signed up")
                AVUser.logInWithUsername(inBackground: user.username!, password: user.password!, block: {(user: AVUser?, error: Error?) in
                    if let user = user {
                        UserDefaults.standard.set(user.username, forKey: "username")
                        UserDefaults.standard.synchronize()
                        let appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
                        appDelegate.login()
                    }
                })
            }else {
                print(error!.localizedDescription)
            }
        }
    }
}

I am writing an app just like Instagram. This is a part of code when people click sign in button. I found it is quite difficult to understand. Why UserDefaults.standard and if successrepeat in the above code ? Thanks for your attention.


Solution

  • did you trying like this .. what is the issue? :

     user.signUpInBackground {(success: Bool, error: Error?) in
                    if success {
                        print("signed up")
                        AVUser.logInWithUsername(inBackground: user.username!, password: user.password!, block: {(user: AVUser?, error: Error?) in
                            if let user = user {
                                UserDefaults.standard.set(user.username, forKey: "username")
                                UserDefaults.standard.synchronize()
                                let appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
                                appDelegate.login()
                            }
                        })
                    }else {
                        print(error!.localizedDescription)
                    }
                }
    

    i seem user.signUpInBackground method called two time . so UserDefaults.standard success repeat