跳到主要内容

账号功能

更新时间:2025-04-02 10:59:11

账号和用户SDK提供了账号能力,包括注册、登录、登出等功能。建议在SDK初始化完成以后再调用登录接口,保证SDK初始化与登录时序性。

内置账号体系

内置账号为RTCX平台提供的账号能力,包括验手机号注册、邮箱注册、验证码登录、自动登录、退出登录、发送验证码、重置密码、修改密码、注销。您在客户端集成后即可使用。

// 导入账号管理SDK头文件
#import <RTCXOpenAccountCloud/RTCXOpenAccountCloud.h>
@protocol RTCXOpenAccountProtocol <NSObject>
/**
* 通过手机验号、邮件 注册
*
* @param req 请求参数对象
* @param onSuccess 成功回调
* @param onError 失败回调
*/
- (void)registerAccountWithReq:(nonnull RTCXOpenAccountRegisterReq *)req onSuccess:(nullable RTCXOpenAccountOnSuccess)onSuccess onError:(nullable RTCXOpenAccountOnError)onError;
/**
* 发送验证码
*
* @param requestParams 请求参数对象
* @param onSuccess 成功回调
* @param onError 失败回调
*/
- (void)sendValidateCodeWithReq:(nonnull RTCXOpenAccountValidateCodeParams *)requestParams onSuccess:(nullable RTCXOpenAccountOnSuccess)onSuccess onError:(nullable RTCXOpenAccountOnError)onError;
/**
* 通过账号密码、手机验证码、OAuthCode授权码 登录
*
* @param requestParams 请求参数对象
* @param onSuccess 成功回调
* @param onError 失败回调
*/
- (void)loginAccountWithReq:(nonnull RTCXOpenAccountLoginParams *)requestParams onSuccess:(nullable RTCXOpenAccountOnSuccess)onSuccess onError:(nullable RTCXOpenAccountOnError)onError;
/**
* 重置密码
*
* @param requestParams 请求参数对象
* @param onSuccess 成功回调
* @param onError 失败回调
*/
- (void)resetPasswordWithReq:(nonnull RTCXOpenAccountResetPwdParams *)requestParams onSuccess:(nullable RTCXOpenAccountOnSuccess)onSuccess onError:(nullable RTCXOpenAccountOnError)onError;
/**
* 修改密码
*
* @param req 请求参数对象
* @param onSuccess 成功回调
* @param onError 失败回调
*/
- (void)modifyPasswordWithReq:(nonnull RTCXOpenAccountModifyPwdReq *)req onSuccess:(nullable RTCXOpenAccountOnSuccess)onSuccess onError:(nullable RTCXOpenAccountOnError)onError;
/**
* 退出登录
*
* @param onSuccess 成功回调
* @param onError 失败回调
*/
- (void)logoutOnSuccess:(nullable RTCXOpenAccountOnSuccess)onSuccess onError:(nullable RTCXOpenAccountOnError)onError;
/**
* 自动登录
*
* @param onSuccess 成功回调
* @param onError 失败回调
*/
- (void)autoLoginOnSuccess:(nullable RTCXOpenAccountOnSuccess)onSuccess onError:(nullable RTCXOpenAccountOnError)onError;
/**
* 用户注销
*
* @param onSuccess 成功回调
* @param onError 失败回调
*/
- (void)logoffWithReq:(nullable RTCXOpenAccountRefreshTokenReq *)req onSuccess:(nullable RTCXOpenAccountOnSuccess)onSuccess onError:(nullable RTCXOpenAccountOnError)onError;

/**
* 是否登录
*
* **@return **是否登录
*/
- (BOOL)isLogin;
/**
* 用户鉴权异常回调,如: code == 30314,token失效
*
*/
- (void)setAuthenticationAbnormalCallback:(nullable RTCXAuthenticationAbnormalCallback)abnormalCallback;
@end

使用 RTCXOpenAccountSDK 宏对象调用示例如下:

用户注册

RTCXOpenAccountRegisterReq *req = [[RTCXOpenAccountRegisterReq alloc] init];
req.accountType = 2;
req.account = self.mobileNum;
req.country = @"CN";
req.pwdType = 3;
req.password = self.passwordTextField.text;
req.verifyCode = self.authCode;
req.loginName = self.mobileNum;
[RTCXService(RTCXOpenAccountProtocol) registerAccountWithReq:req onSuccess:^(id _Nullable data, id _Nullable rawData) {
[self stopLoading];
[self showToast:NSLocalizedString(@"tip_register_success", nil) superView:[UIApplication sharedApplication].keyWindow];
} onError:^(NSError *error) {
self.registerButton.userInteractionEnabled = YES;
[self stopLoading];
if (error.code == 30321) {// 已注册
[self showToast:NSLocalizedString(@"tip_user_exists", nil)];
} else if (error.code == 30315) {// 验证码错误
[self showToast:NSLocalizedString(@"error_verify_code", nil)];
} else {
[NSString stringWithFormat:@"%ld : %@",error.code,error.userInfo[NSLocalizedFailureReasonErrorKey]];
}
}];

自动登录

当调用登录接口完成登录以后,SDK会自动缓存登录用户信息,下次可以直接调用自动登录接口完成登录,切记:自动登录接口需要在SDK初始化完成以后调用,否则会出现异常

__weak typeof(self) weakSelf = self;
[[RTCXIotSmart sharedInstance] setSdkInitCompletedCallback:^{// SDK初始化完成回调
[RTCXService(RTCXOpenAccountProtocol) autoLoginOnSuccess:^(id _Nullable data, id _Nullable rawData) {
DeviceListViewController *deviceVC = [[DeviceListViewController alloc] init];
[self.navigationController pushViewController:deviceVC animated:YES];
} onError:^(NSError * _Nullable error) {
[self showToast:@"登录已过期,请重新使用验证码登录"];
}];
}];

账号密码登录

切记:该接口需要在SDK初始化完成以后调用,否则会出现异常

if![[RTCXIotSmart sharedInstance] sdkInitializationCompleted]{// SDK是否初始化完成
return;// SDK未初始化完成,不允许调用登录接口
}
RTCXOpenAccountLoginParams *req = [[RTCXOpenAccountLoginParams alloc] init];
req.accountType = 2;
req.account = self.accountTextField.text;
req.country = @"CN";
req.pwdType = 1;
req.password = self.passwordTextField.text;
req.loginName = @"accountName";
[RTCXService(RTCXOpenAccountProtocol) loginAccountWithReq:req onSuccess:^(id _Nullable data, id _Nullable rawData) {
self.loginButton.userInteractionEnabled = YES;
[self stopLoading];
DeviceListViewController *deviceVC = [[DeviceListViewController alloc] init];
[self.navigationController pushViewController:deviceVC animated:YES];
[self showToast:@"登录成功"];
} onError:^(NSError *error) {
self.loginButton.userInteractionEnabled = YES;
[self stopLoading];
[self showToast:[NSString stringWithFormat:@"登录失败 : %@", error.userInfo[@"NSLocalizedFailureReason"]]];
}];

验证码登录

切记:该接口需要在SDK初始化完成以后调用,否则会出现异常

if![[RTCXIotSmart sharedInstance] sdkInitializationCompleted]{// SDK是否初始化完成
return;// SDK未初始化完成,不允许调用登录接口
}
RTCXOpenAccountLoginParams *req = [[RTCXOpenAccountLoginParams alloc] init];
req.accountType = 2;
req.account = self.accountTextField.text;
req.country = @"CN";
req.pwdType = 3;
req.password = self.passwordTextField.text;
req.loginName = @"accountName";
[RTCXService(RTCXOpenAccountProtocol) loginAccountWithReq:req onSuccess:^(id _Nullable data, id _Nullable rawData) {
self.loginButton.userInteractionEnabled = YES;
[self stopLoading];
DeviceListViewController *deviceVC = [[DeviceListViewController alloc] init];
[self.navigationController pushViewController:deviceVC animated:YES];
[self showToast:@"登录成功"];
} onError:^(NSError *error) {
self.loginButton.userInteractionEnabled = YES;
[self stopLoading];
[self showToast:[NSString stringWithFormat:@"登录失败 : %@", error.userInfo[@"NSLocalizedFailureReason"]]];
}];

重置密码

RTCXOpenAccountResetPwdParams *req = [[RTCXOpenAccountResetPwdParams alloc] init];
req.account = mobile;
req.verifyCode = verifyCode;
req.password = password;
[RTCXService(RTCXOpenAccountProtocol) resetPasswordWithReq:req onSuccess:^(id _Nullable data, id _Nullable rawData) {
[self showToast:@"重置密码成功"];
[self.navigationController popViewControllerAnimated:YES];
} onError:^(NSError * _Nullable error) {
[self showToast:@"重置密码失败"];
}];

修改密码

RTCXOpenAccountModifyPwdReq *req = [[RTCXOpenAccountModifyPwdReq alloc] init];
req.oldPassword = self.oldPwdTextField.text;
req.updatedPassword = self.updatedPwdTextField.text;
[RTCXService(RTCXOpenAccountProtocol) modifyPasswordWithReq:req onSuccess:^(id _Nullable data, id _Nullable rawData) {
[self showToast:NSLocalizedString(@"tip_modify_pwd_success", nil) superView:[UIApplication sharedApplication].keyWindow];
// 修改密码成功后,平台会发送修改密码的信令通知(类似忘记密码)
[RTCXService(RTCXOpenAccountProtocol) logoutOnSuccess:^(id _Nullable data, id _Nullable rawData) {
UITabBarController *tabbar = self.tabBarController;
[tabbar.navigationController popViewControllerAnimated:YES];
} onError:^(NSError * _Nullable error) {
[self showToast:[NSString stringWithFormat:@"%ld : %@",error.code,error.userInfo[NSLocalizedFailureReasonErrorKey]]];
}];
} onError:^(NSError * _Nullable error) {
[self showToast:[NSString stringWithFormat:@"%ld : %@",error.code,error.userInfo[NSLocalizedFailureReasonErrorKey]]];
}];

发送验证码

RTCXOpenAccountValidateCodeParams *req = [[RTCXOpenAccountValidateCodeParams alloc] init];
req.account = mobile;
[RTCXService(RTCXOpenAccountProtocol) sendValidateCodeWithReq:req onSuccess:^(id _Nullable data, id _Nullable rawData) {
COMMON_PRINTF(@"成功获取到验证码");
if (complete) {
complete(YES, data, nil);
}
} onError:^(NSError * _Nullable error) {
if (complete) {
complete(NO, nil, error);
}
//处理服务端错误
COMMON_PRINTF(@"请求出错了:%@",error);
}];

退出登录

[RTCXService(RTCXOpenAccountProtocol) logoutOnSuccess:^(id  _Nullable data, id  _Nullable rawData) {
[self.navigationController popViewControllerAnimated:YES];
} onError:^(NSError * _Nullable error) {
[self showToast:@"登出失败"];
}];

用户注销

RTCXOpenAccountLogoffReq *req = [[RTCXOpenAccountLogoffReq alloc] init];
req.verifyCode = weakSelf.authCodeTextField.text;
[RTCXService(RTCXOpenAccountProtocol) logoffWithReq:nil onSuccess:^(id _Nullable data, id _Nullable rawData) {
[weakSelf showToast:NSLocalizedString(@"delete_user_success", nil) superView:[UIApplication sharedApplication].keyWindow];
UITabBarController *tabbar = weakSelf.tabBarController;
[tabbar.navigationController popViewControllerAnimated:YES];
} onError:^(NSError * _Nullable error) {
[weakSelf showToast:NSLocalizedString(@"delete_user_failed", nil)];
}];

用户鉴权异常回调,如 token 失效

// 用户鉴权失效监听,收到此回调可以实现退出登陆业务
[RTCXService(RTCXOpenAccountProtocol) setAuthenticationAbnormalCallback:^(NSInteger code, NSString * _Nonnull message) {
if (code == 30314) {// token失效
if ([RTCXService(RTCXOpenAccountProtocol) isLogin]) {
[[self currentViewController].navigationController popToRootViewControllerAnimated:YES];// token过期退出登录
}
}
}];

获取用户全局配置

@protocol RTCXGlobalConfigProtocol <NSObject>
/**
* 获取用户全局配置
*
* @param req 请求参数对象
* @param onSuccess 成功回调
* @param onError 失败回调
*/
- (void)getGlobalConfigWithReq:(nonnull RTCXGlobalConfigReq *)req onSuccess:(nullable RTCXGlobalConfigOnSuccess)onSuccess onError:(nullable RTCXGlobalConfigOnError)onError;
/**
* 获取全局配置
*/
- (RTCXGlobalConfigRsp *)getGlobalConfig;
@end

使用 RTCXOpenAccountSDK 宏对象调用示例如下:

RTCXGlobalConfigReq *req = [[RTCXGlobalConfigReq alloc] init];
req.attributeIdList = @[@(2000)];// 如:2000:直播时长配置.
[RTCXService(RTCXGlobalConfigProtocol) getGlobalConfigWithReq:req onSuccess:^(id _Nullable data, id _Nullable rawData) {
RTCXGlobalConfigRsp *rsp = data;
} onError:^(NSError * _Nullable error) {
}];

账号变动通知

/**
客户端登录账号发生改变通知(如:在另一端修改了密码、登录等,当前端登录账号会收到该消息)
*/
extern NSString *const RTCXCMDAccountChangedNotification;

调用示例如下

// 账号改变通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accountChangedNotification:) name:RTCXCMDAccountChangedNotification object:nil];

- (void)accountChangedNotification:(NSNotification *)notify {
// 退出当前账号登录
}

自有账号体系

登录

通过 authCode 授权码登录,切记:该接口需要在SDK初始化完成以后调用,否则会出现异常

// 导入账号管理SDK头文件
#import <RTCXOpenAccountCloud/RTCXOpenAccountCloud.h>
@protocol RTCXOpenAccountProtocol <NSObject>
/**
* 通过账号密码、手机验证码、OAuthCode授权码 登录
*
* @param requestParams 请求参数对象
* @param onSuccess 成功回调
* @param onError 失败回调
*/
- (void)loginAccountWithReq:(nonnull RTCXOpenAccountLoginParams *)requestParams onSuccess:(nullable RTCXOpenAccountOnSuccess)onSuccess onError:(nullable RTCXOpenAccountOnError)onError;

代码调用示例如下

if![[RTCXIotSmart sharedInstance] sdkInitializationCompleted]{// SDK是否初始化完成
return;// SDK未初始化完成,不允许调用登录接口
}
RTCXOpenAccountLoginParams *req = [[RTCXOpenAccountLoginParams alloc] init];
req.accountType = 6;// OAuthCode授权码登录
req.country = @"CN";
req.pwdType = 4;// OAuthCode授权码
req.password = @"2-SnaB7vsdMSsEiNMNZ7lG-zwLK7l9TuRyXAtezrnN4n9ZyPuorziNy_ZlNSDs5RnWCnnrto0yVIb8umAhhtZQ_1CVeN-0VrzUc4v9nZpg5o1vt0NcXIpmtyMg7n5Zpv";// OAuthCode
[RTCXService(RTCXOpenAccountProtocol) loginAccountWithReq:req onSuccess:^(id _Nullable data, id _Nullable rawData) {
self.loginButton.userInteractionEnabled = YES;
[self stopLoading];
DeviceListViewController *deviceVC = [[DeviceListViewController alloc] init];
[self.navigationController pushViewController:deviceVC animated:YES];
[self showToast:@"登录成功"];
} onError:^(NSError *error) {
self.loginButton.userInteractionEnabled = YES;
[self stopLoading];
[self showToast:[NSString stringWithFormat:@"登录失败 : %@", error.userInfo[@"NSLocalizedFailureReason"]]];
}];