2

I'm super stuck on this because the Syncano docs aren't great at explaining... I'm building a simple chat app with Syncano backend (R.I.P Parse) and all I want to do is save the First Name, Last Name, and a unique username which they can pick. I have managed to connect the user to the server already and can see them in my list of classes, but the additional info for the user doesn't show. I have a view controller called Signup View Controller which handles all the interaction with the labels and button all connected. It is worth mentioning this is my first app and I am a beginner in programming, so I need someone with a little bit experience to give me a few pointers on which its not saving the users Name and unique username.

This is the how I want to send the data to Syncano when pressed:

class SignupViewController: UIViewController {

    var delegate : LoginDelegate?
    var @IBAction func signupPressed(sender: AnyObject) {
    var huddleusernameText = usernameTextField.text
    var firstNameText = firstNameTextField.text
    var lastNameText = lastNameTextField.text
    var passwordText = passwordTextField.text
    var repeatPasswordText = repeatPasswordTextField.text
    var emailText = emailTextField.text

func registerUser(username: String, password: String, first_name: String, last_name: String, email: String)
    {
        SCUser.registerWithUsername(username, password: password)
        { error in
            //handle user registration
            if (error != nil)
            {
            //handle error
            print("Sign Up, error: \(error)")
            }
        }
}

self.signUp(self.getUsername()!, password: self.getPassword()!, finished:
        { error in
    if (error != nil) {
                //handle error
                print("Sign Up, error: \(error)")
    } else
    {
        SCUser.currentUser()
        self.addNewUser()
        self.delegate?.didSignUp()

    }
})

func signUp(username: String, password: String, finished: (NSError!) -> ()) {
    SCUser.registerWithUsername(username, password: password) { error in
        finished(error)
    }
}

// Syncano docs mentions using this, but I'm not sure how to use this   piece of code to store user data...
class MyUserProfile : SCUserProfile {
    var avatar : SCFile? = nil;
    var first_name = ""
    var last_name = ""
    var email = ""
    var username = ""
}

This is something additional

func addNewUser() {
    let user = MyUserProfile()
    user.first_name = firstNameTextField.text!
    user.last_name = lastNameTextField.text!
    user.email = emailTextField.text!
    user.huddle_username = usernameTextField.text!
    user.saveWithCompletionBlock { error in
        //Handle error
    }
}

I will update this question if I run into any new solutions but for now, this is it.

dron22
  • 1,235
  • 10
  • 20
AJ McCaw
  • 23
  • 2
  • something is not right with your code. Please check curly brackets and blocks. e.g. in line `var @IBAction func signupPressed(sender: AnyObject) {` you start a function definition, but I don't see any closing curly brackets. – dron22 Apr 03 '16 at 14:34

1 Answers1

2

You can find more details in the docs: http://docs.syncano.io/docs/ios#section-subclassing-user-and-user-profile, which talks about your own subclass of user profile to use custom fields (like first name and last name).

I know you already have your own custom user profile, but I feel it will be better if I explain it from the beginning.

First, you need to edit user_profile class, easiest would be through the Dashboard and add there first_name and last_name properties.

Next, create your own custom user_profile class:

import syncano_ios

class MyUserProfile: SCUserProfile {
    var first_name = ""
    var last_name = ""
}

Before you can use it, register it somewhere in your app (e.g. in viewDidLoad of your main class, or even in AppDelegate, in func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool method) using:

SCUser.registerClassWithProfileClass(MyUserProfile.self)

You can now write your code that will save first and last name into user profile:

func saveName() {
    guard let profile = SCUser.currentUser().profile as? MyUserProfile else {
        return
    }
    profile.first_name = "Firstname" //get it from e.g. text field
    profile.last_name = "Lastname" //get it from e.g. text field
    profile.saveWithCompletionBlock { error in
    }
}

Now, modify your code. Instead of calling addNewUser, call saveName method:

self.signUp(self.getUsername()!, password: self.getPassword()!, finished:
        { error in
    if (error != nil) {
                //handle error
                print("Sign Up, error: \(error)")
    } else
    {
        self.saveName()
        self.delegate?.didSignUp()

    }
})

The main difference between this code and the one you posted, is that you were trying to create a new user_profile object - that is impossible to do. Instead, Syncano add a new user_profile every time a new user is created. So what you need to do, is just take the user_profile of the new user, and modify it with by adding first and last name.

  • I'm trying to do the same, but when I access currentUser() after registering, it strangely gives me a different user profile. Is Syncano supposed to automatically log in the user when registration completes? – Connor Neville Apr 12 '16 at 17:32
  • @ConnorNeville Best practice would be to logout first manually. We're working on it, and in the next release we will logout automatically before register/login method runs. – Mariusz Wiśniewski Apr 12 '16 at 20:43