现在我们在做开发的时候,基本上现在的每一款app都有登录功能。那也就少不了注册,找回密码等操作。今天要说的就是初学者可以使用parse做为后台的服务器来进行一系列的操作,等以后工作的时候可以用公司的服务器。
我们就先从注册用户开始吧
BmobUser *bUser = [[BmobUser alloc] init]; [bUser setUserName:@"小明"];//必填 [bUser setPassword:@"123456"];//必填 [bUser setObject:@18 forKey:@"age"];//选填 [bUser setEmail:@"123456789@qq.com]"//必填 [bUser signUpInBackgroundWithBlock:^ (BOOL isSuccessful, NSError *error){ if (isSuccessful){ NSLog(@"Sign up successfully"); } else { NSLog(@"%@",error); } }];
有些时候你可能需要在用户注册时发送一封邮件,以确认用户邮箱的真实性。这时,你只需要登录自己的应用管理后台,在应用设置->邮件设置(下图)中把“邮箱验证”功能打开,Bmob云后端就会在用户注册时自动发动一封验证给用户。
username字段是大小写敏感的字段,如果你希望应用的用户名不区分大小写,请在注册登录时进行大小写的统一转换。
接下来就是用户登录了
[BmobUser loginWithUsernameInBackground:@"小明" password:@"123456" block:^(BmobUser *user, NSError *error) { if (user) { [self showMessage:@"登录成功"]; } if (error) { [self showMessage:@"用户名或者密码错误"]; } }];];
获取当前用户
BmobUser *bUser = [BmobUser getCurrentObject]; if (bUser) { //进行操作 }else{ //对象为空时,可打开用户注册界面 } 当然,你也可以用如下的方法清除缓存用户对象: [BmobUser logout];
更新用户资料
BmobUser *bUser = [BmobUser getCurrentUser]; //更新number为30 [bUser setObject:@30 forKey:@"number"]; [bUser updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) { NSLog(@"error %@",[error description]); }];
在更新用户信息时,如果用户邮箱有变更并且在管理后台打开了邮箱验证选项的话,Bmob云后端同样会自动发动一封邮件验证信息给用户。
旧密码重置密码
BmobUser *user = [BmobUser getCurrentUser]; [user updateCurrentUserPasswordWithOldPassword:@"old password" newPassword:@"new password" block:^(BOOL isSuccessful, NSError *error) { if (isSuccessful) { //用新密码登录 [BmobUser loginInbackgroundWithAccount:@"name" andPassword:@"new password" block:^(BmobUser *user, NSError *error) { if (error) { NSLog(@"login error:%@",error); } else { NSLog(@"user:%@",user); } }]; } else { NSLog(@"change password error:%@",error); } }];
邮箱重置密码
[BmobUser requestPasswordResetInBackgroundWithEamil:@"xxxx@qq.com"];
密码重置流程如下:
邮箱验证
BmobUser *user = [BmobUser getCurrentUser]; //应用开启了邮箱验证功能 if ([user objectForKey:@"emailVerified"]) { //用户没验证过邮箱 if (![[user objectForKey:@"emailVerified"] boolValue]) { [user verifyEmailInBackgroundWithEmailAddress:@"xxxxxxxxxx"]; } }
手机注册登录,一键注册登录
[BmobUser signOrLoginInbackgroundWithMobilePhoneNumber:mobilePhoneNumber andSMSCode:smsCode block:^(BmobUser *user, NSError *error) { if (user) { NSLog(@"%@",user); } else { NSLog(@"%@",error); } }];
如果希望在用手机注册时为用户添加密码或者其它信息,可以使用以下代码实现:
BmobUser *buser = [[BmobUser alloc] init]; buser.mobilePhoneNumber = @"15123456789"; buser.password = @"123"; buser.email = @"xxx@gmail.com" [buser signUpOrLoginInbackgroundWithSMSCode:@"6位验证码" block:^(BOOL isSuccessful, NSError *error) { if (error) { NSLog(@"%@",error); } else { BmobUser *user = [BmobUser getCurrentUser]; NSLog(@"%@",[BmobUser getCurrentUser]); } }];
帐号密码登录
[BmobUser loginInbackgroundWithAccount:account andPassword:password block:^(BmobUser *user, NSError *error) { if (user) { NSLog(@"%@",user); } else { NSLog(@"%@",error); } }];
手机号验证码登录
[BmobUser loginInbackgroundWithMobilePhoneNumber:mobilePhoneNumber andSMSCode:smsCode block:^(BmobUser *user, NSError *error) { if (user) { NSLog(@"%@",user); } else { NSLog(@"%@",error); } }];
手机验证码重置密码
[BmobUser resetPasswordInbackgroundWithSMSCode:smsCode andNewPassword:newPassword block:^(BOOL isSuccessful, NSError *error) { if (isSuccessful) { NSLog(@"%@",@"重置密码成功"); } else { NSLog(@"%@",error); } }];
绑定手机号
//验证 [BmobSMS verifySMSCodeInBackgroundWithPhoneNumber:mobilePhoneNumber andSMSCode:smsCode resultBlock:^(BOOL isSuccessful, NSError *error) { if (isSuccessful) { //修改绑定手机 BmobUser *buser = [BmobUser getCurrentUser]; buser.mobilePhoneNumber = mobilePhoneNumber; [buser setObject:[NSNumber numberWithBool:YES] forKey:@"mobilePhoneNumberVerified"]; [buser updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) { if (isSuccessful) { NSLog(@"%@",buser); } else { NSLog(@"%@",error); } }]; } else { NSLog(@"%@",error); } }];
请求发送短信内容
[BmobSMS requestSMSInbackgroundWithPhoneNumber:@"phoneNumber" Content:@"您的XX服务还有XX时间到期,请及时续费。" andSendTime:@"2015-07-01 10:59:00" resultBlock:^(int number, NSError *error) { if (error) { NSLog(@"%@",error); } else { NSLog(@"smsId:%d",number); } }];
获取短信验证码
//请求验证码 [BmobSMS requestSMSCodeInBackgroundWithPhoneNumber:mobilePhoneNumber andTemplate:@"test" resultBlock:^(int number, NSError *error) { if (error) { NSLog(@"%@",error); } else { //获得smsID NSLog(@"sms ID:%d",number); } }];
验证短信验证码
//验证 [BmobSMS verifySMSCodeInBackgroundWithPhoneNumber:mobilePhoneNumber andSMSCode:smsCode resultBlock:^(BOOL isSuccessful, NSError *error) { if (isSuccessful) { NSLog(@"%@",@"验证成功,可执行用户请求的操作"); } else { NSLog(@"%@",error); } }]; }
虽然代码量有点多,但是逻辑不是很难,如果能够按照我的代码来的话,这一套用户的登录和注册应该可以搞定。