- 推荐链接 :https://www.bmob.cn/?invite_code=$W8S
- 推荐码:$W8S
使用 Bmob 后端云,不用自己写 API 接口,只需要定义数据表及相关字段即可实现用户注册、登录、修改用户信息功能。
具体怎么创建应用,大家看官网介绍就行,这里简单说下数据表。
创建应用后,应用中的云数据库默认存在一个用户表_User,且相应的存在一些默认字段,如:帐号username
,密码password
,手机号mobilePhoneNumberVer
/mobilePhoneNumber
,邮箱emailVerified
;我们开发者可以自定义往里添加需要的字段,如:昵称nickName
。
示例图:
代码示例
1、注册
/// 注册
+ (void)registerWithName:(NSString *)name password:(NSString *)password parameter:(NSDictionary *)dict complete:(void (^)(BOOL success, NSError *error))complete
{
BmobUser *user = [[BmobUser alloc] init];
[user setUsername:name];
[user setPassword:password];
[dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
[user setObject:obj forKey:key];
}];
[user signUpInBackgroundWithBlock:^(BOOL isSuccessful, NSError *error) {
if (complete) {
complete(isSuccessful, error);
}
}];
}
运行示例
[UserModel registerWithName:@"devZhang" password:@"123456" parameter:@{@"nickName":@"番薯大佬"} complete:^(BOOL success, NSError * _Nonnull error) {
NSLog(@"success %@, error = %@", @(success), error.description);
}];
------------------------------------------------
success 1, error = (null)
2、登录
/// 登录
+ (void)loginWithName:(NSString *)userName password:(NSString *)password complete:(void (^)(BmobUser *user, NSError *error))complete
{
[BmobUser loginWithUsernameInBackground:userName password:password block:^(BmobUser *user, NSError *error) {
if (complete) {
complete(user, error);
}
}];
}
运行示例
[UserModel loginWithName:@"devZhang" password:@"123456" complete:^(BmobUser * _Nonnull user, NSError * _Nonnull error) {
NSLog(@"user %@, error = %@", user, error.description);
}];
------------------------------------------------
user
className = _User;
username = devZhang;
mobilePhoneNumber = (null);
email = (null);
objectId = 2bb9f3db62;
createdAt = 2020-02-09 02:40:45 +0000;
updatedAt = 2020-02-09 02:40:45 +0000;
data = {
createdAt = "2020-02-09 10:40:45";
nickName = "\U756a\U85af\U5927\U4f6c";
username = devZhang;
};
3、修改用户信息
/// 修改用户信息
+ (void)modifyWithParameter:(NSDictionary *)dict complete:(void (^)(BOOL success, NSError *error))complete
{
BmobUser *user = UserModel.user;
if (user == nil) {
if (complete) {
NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:-1 userInfo:@{@"message":@"用户不存在"}];
complete(NO, error);
}
return;
}
[dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
[user setObject:obj forKey:key];
}];
[user updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
if (complete) {
complete(isSuccessful, error);
}
}];
}
运行示例
[UserModel modifyWithParameter:@{@"nickName":@"大番薯"} complete:^(BOOL success, NSError * _Nonnull error) {
NSLog(@"success %@, error = %@", @(success), error.description);
}];
------------------------------------------------
success 1, error = (null)
4、获取当前用户信息
/// 当前用户
+ (BmobUser *)user
{
BmobUser *user = [BmobUser currentUser];
return user;
}
运行示例
BmobUser *user = UserModel.user;
NSLog(@"user %@", user);
------------------------------------------------
user
className = _User;
username = devZhang;
mobilePhoneNumber = (null);
email = (null);
objectId = 2bb9f3db62;
createdAt = 2020-02-09 02:40:45 +0000;
updatedAt = 2020-02-09 02:42:02 +0000;
data = {
createdAt = "2020-02-09 10:40:45";
nickName = "\U5927\U756a\U85af";
updatedAt = "2020-02-09 10:42:02";
username = devZhang;
};