A, attributes,

BmobUser has several specific properties in addition to the properties it inherits from BmobObject:

  • Username: indicates the username (required).
  • Password: indicates the user password (required).
  • Email: Indicates the optional email address of a user.

BmobUser automatically handles the functions required for user account management.

-(void)setUsername:(NSString *)username; // user name, required -(void)setPassword:(NSString*)password; // password, required -(void)setEmail:(NSString *)email; // set the mailbox -(void)setObject:(id)obj forKey:(id)key; // set the value of an attribute -(id)objectForKey:(id)key; // Get the value of an attributeCopy the code

Second, the registration

One of the most common features of an application is to register users. Registering users with BmobUser is not complicated, as shown in the following example:

BmobUser *bUser = [[BmobUser alloc] init]; [bUser setUsername:self.nameTextField.text]; [bUser setPassword:self.passwordTextField.text]; [bUser signUpInBackgroundWithBlock:^ (BOOL isSuccessful, NSError *error){ if (isSuccessful){ NSLog(@"Sign up successfully");  NSString *result = [NSString StringWithFormat: @ "\ n registration user name: % @ \ n password: % @ \ n to try to log in", the self. The nameTextField. The text, the self. PasswordTextField. Text]; [self showResultInfo:result];} else {NSLog(@"%@",error); [self showResultInfo:@" %@",error);}}];Copy the code

Note:

  • In some cases, you may need to send an email to verify the user’s email address when they sign up. Just log in to your app and turn on “Email Authentication” in App Settings > Email Settings. The Bmob backend will automatically send an authentication message to users when they register.

  • The username field is case-sensitive. If you want the application username to be case-insensitive, convert the case during registration and login.

Third, the login

Once the user has registered, they need to be able to log into their account to use the application later. And the way to do that is to use method one


[BmobUser loginWithUsernameInBackground:self.nameTextField.text password:self.passwordTextField.text];

Copy the code

Method 2

[BmobUser loginWithUsernameInBackground:self.nameTextField.text password:self.passwordTextField.text block:^(BmobUser *user, NSError *error) { if (user) { NSString *result = [NSString StringWithFormat: @ "login success \ n user name: % @ \ n password: % @ \ n to try to get the current user" to the user. The username, user, password]; [self showResultInfo: result]; } else {NSLog (@ "% @", the error); [self showResultInfo: @ "login failed, check the console messages"];}}];Copy the code

As you can see, Bmob provides a lot of methods to call as needed

4. Obtain the current user

Each time you log in successfully, there will be a cached user object on the local disk as the current user. You can retrieve this cached user object to log in:

BmobUser *user = [BmobUser currentUser]; If (user) {// operate NSString *result = [NSString StringWithFormat: @ "login success \ n user name: % @ \ n password: % @ \ n to try to update the user" to the user. The username, user, password]; [self showResultInfo:result]; }else{self showResultInfo:@" There is no user "]; }Copy the code

Of course, you can also clear the cached user object using the following method:

[BmobUser logout];
Copy the code

Update users

When the user logs in successfully, there is a cached user object locally. If the developer wants to change an attribute of the current user, the following code can be used:

BmobUser *bUser = [BmobUser currentUser]; if (bUser) { [bUser setObject:self.nameTextField.text forKey:@"username"]; [bUser updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {if (isSuccessful) {[self showResultInfo:@" update user successfully "]; }else{NSLog(@"error %@",[error description]); [self showResultInfo:@" error %@",[error description]); }else{[self showResultInfo:@" not currently login "]; }Copy the code

In general, there is a problem with using the current user object for data updates. If the time between the last login and the current login is too long, the Token stored in the local store may expire, causing the user to fail to update the information. In this case, the user needs to log in again and update the information only after the login is successful.

When updating user information, the Bmob backend will automatically send an email authentication message to the user if the user’s email is changed and the email authentication option is enabled in the admin background.

6. Query users

To query a user, just specify the BmobUser class as follows:

BmobQuery *query = [BmobUser query]; [query whereKey:@"username" equalTo:self.nameTextField.text]; [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) { if (error) { NSLog(@"%@",error); [self showResultInfo:@" query error, query console info "]; }else{if (array.count>0) {for (BmobUser *user in array) { NSLog(@"objectid %@", user.objectid); Nsstrings * result = [nsstrings stringWithFormat: @ "query success \ n user name: % @ \ n user id: % @", the user. The username, user, objectId); [self showResultInfo:result];}}else{[self showResultInfo:result];}}else{Copy the code

View the user table in the background of Bmob

7. Change the password

The interface for resetting a new password using the old password is as follows: Modify

BmobUser *user = [BmobUser currentUser]; [user updateCurrentUserPasswordWithOldPassword:self.nameTextField.text newPassword:self.passwordTextField.text block:^(BOOL isSuccessful, NSError *error) { if (isSuccessful) { [self showResultWithLoginInfo:user.username]; } else {NSLog(@"change password error:%@",error); [self showResultInfo:@" change password error:%@",error);}}];Copy the code

Log in after the modification is successful

/ / login with new password [BmobUser loginInbackgroundWithAccount: result andPassword: self. PasswordTextField. The text block: ^ (BmobUser * user, NSError *error) {if (error) {NSLog(@"login error:%@",error); [self showResultInfo:@" login error:%@",error);  } else { NSLog(@"user:%@",user); Nsstrings * result = [nsstrings stringWithFormat: @ "login successful \ n user name: % @ \ n user ID: % @ \ n", the user. The username, user, objectId);  [self showResultInfo:result]; } }];Copy the code

Eight, email retrieve (modify) password

The premise is that the mailbox was added during the registration operation

Once you introduce a password system, there are bound to be cases where users forget their passwords. In this case, we provide a way for users to reset their passwords securely.

The password reset process is as simple as asking the user to enter their registered email address

[BmobUser requestPasswordResetInBackgroundWithEmail:self.nameTextField.text block:^(BOOL isSuccessful, NSError *error) {if (isSuccessful) {[self showResultInfo:@" %@",error);}else{NSLog(@"%@",error); [self showResultInfo:@" send failed, please check console info "];}}];Copy the code

The following is the mailbox that is not bound when mailbox authentication and registration are not enabled

The password reset process is as follows:

  1. Users enter their E-mail and ask to reset their password.
  2. Bmob sends an email containing a special password reset connection to their mailbox.
  3. Users follow the wizard to click on the Reset Password connection, opening a special Bmob page where they are prompted to enter a new password.
  4. The user’s password has been reset to the newly entered password.

Nine,

Note:

  1. Email authentication needs to be enabled in the Bmob background
  2. Bmob background User table can add fields to improve User information
  3. To operate information such as password through email, users are required to verify the email before registration; otherwise, the operation will fail

Bmob access specific operations can be viewed in the last article iOS independent developers using Bmob third-party background services on the synchronization of this article to personal blog code portal, welcome star, interested can leave a message together to discuss. Search the public account JacerooChu or scan the QR code below to discuss and get more information.