User registration with different memberships. SWIFT

I am trying to offer the option for the user to be either a rider or a driver, so I added a switch in the Sign up page so they can decide what membership to choose. So when I am adding user[“isDriver”] =switch.on an error appears saying Type ‘BackendlessUser’ has no subscript membership. I am not sure but I guess that what I have to do is add a custom property in the console so the app recognizes a new property of the user… nevertheless I have tried a million types and I can’t add it properly. Any help or suggestion? Thanks

Hello!

Obviously, it should be a boolean property.
You should follow instructions from here in order to work with custom user properties: https://backendless.com/documentation/users/ios/users_user_properties.htm
Alex

thanks

Thank you Alexandr, but still I added a new property to the console with name membership, and type boolean, with a default value false, but still I don’t what to do so the line of code user[“isDriver”] =switch.on identifies that new property

Your user property should have the same name as it has on code. So, it should be “isDriver”.

As I understand your idea, logic should be the following:

  1. Retrieve existing user/create a new one
  2. Find out if the user “isDriver” or not
  3. Set user property to YES or NO
  4. Save/Update the user
    Here is a small code sample:








    NSNumber *isDriver = @YES;  

    BackendlessUser *user = [backendless.userService login:@"user@example.com" password:@"user_password" ];

    [ user setProperty:@"isDriver" object:isDriver ];

    [backendless.userService update:user];

hope it helps,
Alex

I am using SWIFT, so the way i did it was this one

        var isDriver = true

        isDriver = `switch`.on

        

        backendless.userService.signUp(username.email, user.password) in this line I have an error Value of type UserServicehas no member sign up

        user.setProperty("isDriver", object: isDriver)

        backendless.userService.update(user)

The thing is that i’m trying to code a sign up page but Im having some issues. this is the code of the ViewController, you may be able to help me and I will really appreciate it. the appId, Secretkey and version number are in the app delegate along with the appInit, and backendless.sharedInstance, although that one is also in view controller.

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

var backendless = Backendless.sharedInstance()

var isDriver = true

func displayAlert(title: String, message: String) {

    

    

    

    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)

    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

    self.presentViewController(alert, animated: true, completion: nil)

    

}





@IBOutlet var username: UITextField!





@IBOutlet var password: UITextField!





@IBOutlet var `switch`: UISwitch!



@IBOutlet var driverLabel: UILabel!



@IBOutlet var riderLabel: UILabel!



@IBAction func signUp(sender: AnyObject) {




    if username.text == "" || password.text == ""{

     

        displayAlert("Missing Field(s)", message: "Username and Password are required")

        

    }else {

        

        var user = BackendlessUser()

        user.email = username.text

        user.password = password.text

        

        var isDriver = true

        isDriver = `switch`.on

        

        backendless.userService.signUp(username.email, user.password)

        user.setProperty("isDriver", object: isDriver)

        backendless.userService.update(user)

        

        var registeredUser = self.backendless.userService.signUp(user)

        

        

    }

    

}



@IBOutlet var signUpButton: UIButton!






@IBOutlet var toggleSignUpButton: UIButton!



@IBAction func toggleSignUp(sender: AnyObject) {

    

}



override func viewDidLoad() {

    

    super.viewDidLoad()

    

    

    




    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "DismissKeyboard")

    view.addGestureRecognizer(tap)

    

    self.username.delegate = self;

    self.password.delegate = self;

    

   

    

    

    // Do any additional setup after loading the view, typically from a nib.

}







func DismissKeyboard(){

    //Causes the view (or one of its embedded text fields) to resign the first responder status.

    view.endEditing(true)

}




override func didReceiveMemoryWarning() {

    super.didReceiveMemoryWarning()

    

    // Dispose of any resources that can be recreated.

    

}



func textFieldShouldReturn(textField: UITextField) -> Bool {

    self.view.endEditing(true)

    return false

}

}

What is ‘signUp’? UserService class doesn’t have that method.

Maybe do you need ‘login’ method?

yeah, I know, I fixed it already. Nevertheless, the registration is still not working as it’s supposed too. I added, email, password and then an option either driver or rider, but it is giving me the disp[layAlertMessage, not registering as it is supposed to.

Here is the code

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

var backendless = Backendless.sharedInstance()







func displayAlert(title: String, message: String) {

    

    

    

    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)

    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

    self.presentViewController(alert, animated: true, completion: nil)

    

}





@IBOutlet var username: UITextField!





@IBOutlet var password: UITextField!





@IBOutlet var `switch`: UISwitch!



@IBOutlet var driverLabel: UILabel!



@IBOutlet var riderLabel: UILabel!



@IBAction func signUp(sender: AnyObject) {




    if username.text == "" || password.text == ""{

     

        displayAlert("Missing Field(s)", message: "Username and Password are required")

        

    }else {

        

        Types.tryblock({ () -> Void in

            let user = BackendlessUser()

            user.email = self.username.text

            user.password = self.password.text

            

            var isDriver = true

            isDriver = self.`switch`.on

            

            self.backendless.userService.registering(user)

            user.setProperty("isDriver", object: isDriver)

            self.backendless.userService.update(user)

            




            

            let registeredUser = self.backendless.userService.registering(user)

            

            print("User has been registered")




            }, catchblock: { (exception) -> Void in

                

                self.displayAlert("Sign up Failed", message: "Please try again")

        })

       

        

        

        

        

        

     //let registeredUser = self.backendless.userService.registering(user)

    }

    

}



@IBOutlet var signUpButton: UIButton!






@IBOutlet var toggleSignUpButton: UIButton!



@IBAction func toggleSignUp(sender: AnyObject) {

    

}



override func viewDidLoad() {

    

    super.viewDidLoad()

    

    

    




    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "DismissKeyboard")

    view.addGestureRecognizer(tap)

    

    self.username.delegate = self;

    self.password.delegate = self;

    

   

    

    

    // Do any additional setup after loading the view, typically from a nib.

}







func DismissKeyboard(){

    //Causes the view (or one of its embedded text fields) to resign the first responder status.

    view.endEditing(true)

}




override func didReceiveMemoryWarning() {

    super.didReceiveMemoryWarning()

    

    // Dispose of any resources that can be recreated.

    

}



func textFieldShouldReturn(textField: UITextField) -> Bool {

    self.view.endEditing(true)

    return false

}

}

I am doing the registration page at this point, the try to move on to log in

Javier, please try this:

    func updateCurrentUser() {
        
        let email = "YOUR_EMAIL"
        let password = "YOUR_PASSWORD"
        
        var error: Fault?
        backendless.userService.login(email, password: password, error:&error) as BackendlessUser
        if error == nil {
            print("User has been logged in: \(backendless.userService.currentUser)")
            
            backendless.userService.currentUser.setProperty(""isDriver", object: yes)
            
            backendless.data.update(
                backendless.userService.currentUser,
                response: {(response:AnyObject!) -> Void in
                print("User has been updated: \(response)")
                },
                error: {(fault:Fault!) -> Void in
                    print("saveSelfUser: (2) server reported an error: \(fault)")
                }
            )
          }
        else {
            print("saveSelfUser: (1) server reported an error: \(error)")
        }
    }


This is for log in right? With the code that i copy paste the intent is to register new users, they have a switch that is the isDriver and then they have to add email and password

Please see this registration sample.

Hope our BlogFeatureDay project will helpful for you.

That one is the one I did, but instead of having that function I have it within the func signup as it is the action outlet. In my code the same lines are there, but it is not working as it is supposed to, any idea how to make it work the way it is as of now?

No it seems it’s working but i had to comment out the var isDriver in order to make it work, any idea how could I give the option for the user to register as one or another? I have created the isDriver property as a boolean already in the backendless dashboard, but it won’t work…