Log In
  • The app User object manages information about current application user.

  • This object is optional, and it is used when the app needs to authenticate the user.

  • The authentication provides data about user’s identity: a unique user ID or similar.

  • The user login and logout calls are managed by the app.

  • To access existing User object, use currentUser property:

let theUser: MOCAUser? = MOCA.currentInstance().currentUser
// Access authenticated user object
MOCAUser * theUser = [[MOCA currentInstance] currentUser];
if (theUser)
{
    // ...
}
  • To authenticate a new user, use login:id call:

🚧

User will persist across restarts

User will stay "Logged in" in MOCA SDK until you explicitly call the logout method

let user = MOCA.currentInstance().login("[email protected]")
MOCAUser * currentUser = [[MOCA currentInstance] login:@"UNIQUE_USER_ID"];
if (!currentUser) {
   NSLog(@"MOCA User login failed");
}
  • To logout a user, call:
user.logout()
[user logout];
  • The user object holds a collection of properties. Property-value pairs can be set and retrieved.
  • Each user object is persisted both locally and in the cloud.
  • It is recommended to perform as many sets as desired and then
    just invoke a single save operation to persist them to the MOCA cloud.
guard let user = MOCA.currentUser() else {
  print("MOCA User login failed")
  return;
}
user.setValue("male", forProperty: "gender")
user.setValue(Int(1975), forProperty: "birth_year")
// Asynchronously save the user object to the cloud.
user.save({(_ user: MOCAUser?, _ error: Error?) -> Void in
           if error != nil {
             print("Save user failed: (%@)", error ?? "Error not available")
           }
          })
MOCAUser * theUser = [[MOCA currentInstance] currentUser];    
if (theUser) {
    [theUser setValue:@"male" forProperty:@"gender"];
    [theUser setValue:[NSNumber numberWithInt:1975] forProperty:@"birth_year"];    // Asynchronously save the user object to the cloud.
    [theUser saveWithBlock:^(MOCAUser *user, NSError *error)
        {
          if (error) NSLog(@"Save user failed: %@", error);
        }
    ];
}