用户账号

更新时间:2022-02-17 06:44:56下载pdf

智慧社区 SDK 提供移动端密码登录体系。

出于对用户信息的隐私安全考虑,涂鸦对手机号码验证码的机制做了优化和账号限制。若您想继续使用,请联系您的涂鸦客户经理或 提交工单开通该服务。

注册手机号码账号

手机号码密码注册,包括获取验证码接口和注册账号接口。

获取验证码

接口说明

- (void)sendVerifyCodeWithUserName:(NSString *)userName
                            region:(NSString *_Nullable)region
	               countryCode:(NSString *)countryCode
                     	      type:(NSInteger)type
		           success:(TYSuccessHandler)success
		           failure:(TYFailureError)failure;

示例代码

ObjC:

[[TuyaSmartUser sharedInstance] sendVerifyCodeWithUserName:@"yourUsename"
                                                    region:region
                                               countryCode:@"yourCountryCode"
                                                      type:1
                                                   success:^{
                                                      NSLog(@"sendVerifyCode success");
                                                   } failure:^(NSError *error) {
                                                      NSLog(@"sendVerifyCode failure: %@", error);
}];

Swift:

TuyaSmartUser.sharedInstance().sendVerifyCode(withUserName: "yourUsename", region: region, countryCode: "yourCountryCode", type: 1, success: {
    print("sendVerifyCode success")
}, failure: { error in
    if let error = error {
        print("sendVerifyCode failure: \(error)")
    }
})

参数说明

参数 说明
userName 手机号码
region 区域,默认填写:“” 即可
countryCode 国家码,目前仅支持“86”,即中国大陆
type 发送验证码类型:1:注册验证码, 3: 重置密码时⽤
callback 回调

手机号码密码注册

接口说明

手机号码密码注册

- (void)registerByPhone:(NSString *)countryCode
	    phoneNumber:(NSString *)phoneNumber
               password:(NSString *)password
		   code:(NSString *)code
		success:(nullable TYSuccessHandler)success
		failure:(nullable TYFailureError)failure;

参数说明

参数 说明
countryCode 国家码,目前仅支持“86”,即中国大陆
phoneNumber 电话号码
passwd 密码
code 验证码
callback 回调

示例代码

ObjC:

[[TuyaSmartUser sharedInstance] registerByPhone:@"your_country_code" phoneNumber:@"your_phone_number" password:@"your_password" code:@"verify_code" success:^{
    NSLog(@"register success");
} failure:^(NSError *error) {
    NSLog(@"register failure: %@", error);
}];

Swift:

TuyaSmartUser.sharedInstance()?.register(byPhone: "your_country_code", phoneNumber: "your_phone_number", password: "your_password", code: "verify_code", success: {
    print("register success")
}, failure: { (error) in
    if let e = error {
        print("register failure: \(e)")
    }
})

登录手机号码账号

接口说明

使用手机号码和密码登录。

- (void)loginByPhone:(NSString *)countryCode
       phoneNumber:(NSString *)phoneNumber
          password:(NSString *)password
           success:(nullable TYSuccessHandler)success
           failure:(nullable TYFailureError)failure;

参数说明

参数 说明
countryCode 国家码,目前仅支持“86”,即中国大陆
phone 手机号码
passwd 登陆密码
callback 登陆回调接口

示例代码

ObjC:

[[TuyaSmartUser sharedInstance] loginByPhone:@"your_country_code" phoneNumber:@"your_phone_number" password:@"your_password" success:^{
		NSLog(@"login success");
} failure:^(NSError *error) {
		NSLog(@"login failure: %@", error);
}];

Swift:

TuyaSmartUser.sharedInstance()?.login(byPhone: "your_country_code", phoneNumber: "your_phone_number", password: "your_password", success: {
    print("login success")
}, failure: { (error) in
    if let e = error {
        print("login failure: \(e)")
    }
})

重置账号密码

手机号码重置密码流程分为以下两步:获取手机验证码( API 参考“手机号码密码注册”的第一个接口) - 重置密码。

接口说明

手机号码重置密码

- (void)resetPasswordByPhone:(NSString *)countryCode
                 phoneNumber:(NSString *)phoneNumber
                 newPassword:(NSString *)newPassword
                        code:(NSString *)code
                     success:(nullable TYSuccessHandler)success
                     failure:(nullable TYFailureError)failure;

参数说明

参数 说明
countryCode 国家码,目前仅支持“86”,即中国大陆
phone 手机号码
code 验证码
newPasswd 新密码
callback 回调

示例代码

ObjC:

- (void)resetPasswordByPhone {
	[TuyaSmartUser sharedInstance] resetPasswordByPhone:@"your_country_code" phoneNumber:@"your_phone_number" newPassword:@"your_password" code:@"verify_code" success:^{
		NSLog(@"resetPasswordByPhone success");
	} failure:^(NSError *error) {
		NSLog(@"resetPasswordByPhone failure: %@", error);
	}];
}

Swift:

func resetPasswordByPhone() {
    TuyaSmartUser.sharedInstance()?.resetPassword(byPhone: "your_country_code", phoneNumber: "your_phone_number", newPassword: "your_password", code: "verify_code", success: {
        print("resetPasswordByPhone success")
    }, failure: { (error) in
        if let e = error {
            print("resetPasswordByPhone failure: \(e)")
        }
    })
}

发送不同类型验证码

通过不同的类型获取对应的验证码。

接口说明

验证验证码的合法性,用于登录、注册、重置密码 时验证码的校验

- (void)checkCodeWithUserName:(NSString *)userName
                       region:(NSString *_Nullable)region
                  countryCode:(NSString *)countryCode
                         code:(NSString *)code
                         type:(NSInteger)type
                      success:(TYSuccessBOOL)success
                      failure:(TYFailureError)failure;

参数说明

参数 说明
userName 用户名(手机号码)
region 区域,默认填写:“” 即可
countryCode 国家码,目前仅支持“86”,即中国大陆
code 验证码
type 类型:
1: 注册时验证码验证用
2: 验证码登录时用
3: 重置密码时用
8: 验证码注销时用
callback 回调

示例代码

ObjC:

[[TuyaSmartUser sharedInstance] checkCodeWithUserName:@"email_or_phone_number"
                                               region:@"region"
                                          countryCode:@"your_country_code"
                                                 code:@"verify_code"
                                                 type:1
                                          success:^(BOOL result) {
		                                       if (result) {
		                                 		NSLog(@"valid code!");
                                                         } else {
				                                 NSLog(@"invalid code!");
                                                        }
                                          } failure:^(NSError *error) {
		                                      NSLog(@"check code failure: %@", error);
}];

Swift:

TuyaSmartUser.sharedInstance()?.checkCode(withUserName: "email_or_phone_number", region: "region", countryCode: "your_country_code", code: "verify_code", type: type, success: { (result) in
		if result {
				print("valid code!")
		} else {
				print("invalid code!")
		}
}, failure: { (error) in
		if let error = error {
				print("check code failure: \(error)")
		}
})

账号管理

涂鸦 IoT 平台支持对用户账号信息的管理,包括账号绑定、修改用户信息、会话过期处理、账号注销等。

修改用户头像

接口说明

用于上传用户自定义的头像。

- (void)updateHeadIcon:(UIImage *)headIcon
               success:(nullable TYSuccessHandler)success
               failure:(nullable TYFailureError)failure;

参数说明

参数 说明
file 用户头像图片文件
callback 回调

示例代码

Objc:

- (void)updateHeadIcon:(UIImage *)headIcon {
	[[TuyaSmartUser sharedInstance] updateHeadIcon:headIcon success:^{
		NSLog(@"update head icon success");
	} failure:^(NSError *error) {
		NSLog(@"update head icon failure: %@", error);
	}];
}

Swift:

func updateHeadIcon(_ headIcon: UIImage) {
    TuyaSmartUser.sharedInstance()?.updateHeadIcon(headIcon, success: {
        print("update head icon success")
    }, failure: { (error) in
        if let e = error {
            print("update head icon failure: \(e)")
        }
    })
}

修改账号昵称

接口说明

修改昵称

- (void)updateNickname:(NSString *)nickName
               success:(nullable TYSuccessHandler)success
               failure:(nullable TYFailureError)failure;

参数说明

参数 说明
name 昵称
callback 回调

示例代码

Objc:

- (void)modifyNickname:(NSString *)nickname {
	[[TuyaSmartUser sharedInstance] updateNickname:nickname success:^{
		NSLog(@"updateNickname success");
	} failure:^(NSError *error) {
		NSLog(@"updateNickname failure: %@", error);
	}];
}

Swift:

func modifyNickname(_ nickName: String) {
    TuyaSmartUser.sharedInstance()?.updateNickname(nickName, success: {
        print("updateNickname success")
    }, failure: { (error) in
        if let e = error {
            print("updateNickname failure: \(e)")
        }
    })
}

同步用户信息

当用户信息发生了变更,如修改了用户头像、昵称等,需要调用同步用户信息接口,保持用户信息是最新的状态。如果多台设备同时登录,一台设备修改了用户信息。另外一台设备也需要同步用户信息,可以在查看用户信息时调用同步接口,同步最新的用户信息。

接口说明

用于同步用户信息

- (void)updateUserInfo:(nullable TYSuccessHandler)success
               failure:(nullable TYFailureError)failure;

参数说明

参数 说明
callback 回调

示例代码

- (void)updateUserInfo {
    [[TuyaSmartUser sharedInstance] updateUserInfo:^{
		NSLog(@"update userInfo success");
	} failure:^(NSError *error) {
		NSLog(@"update userInfo failure: %@", error);
	}];
}
func updateUserInfo() {
    TuyaSmartUser.sharedInstance()?.updateUserInfo({
        print("updateNickname success")
    }, failure: { (error) in
        if let e = error {
            print("updateNickname failure: \(e)")
        }
    })
}

账号退出登录

接口描述

用户账号切换的时候需要调用退出登录接口

- (void)loginOut:(nullable TYSuccessHandler)success
         failure:(nullable TYFailureError)failure;

参数说明

参数 说明
success 接口发送成功回调
failure 接口发送失败回调,error 表示失败原因

示例代码

Objc:

- (void)loginOut {
	[TuyaSmartUser sharedInstance] loginOut:^{
		NSLog(@"logOut success");
	} failure:^(NSError *error) {
		NSLog(@"logOut failure: %@", error);
	}];
}

Swift:

func loginOut() {
    TuyaSmartUser.sharedInstance()?.loginOut({
        print("logOut success")
    }, failure: { (error) in
        if let e = error {
            print("logOut failure: \(e)")
        }
    })
}

注销账号

接口描述

一周后账号才会永久停用并删除以下您账户中的所有信息,在此之前重新登录,则您的停用请求将被取消。

- (void)cancelAccount:(nullable TYSuccessHandler)success
              failure:(nullable TYFailureError)failure;

参数说明

参数 说明
success 接口发送成功回调
failure 接口发送失败回调,error 表示失败原因

示例代码

Objc:

- (void)cancelAccount {
	[TuyaSmartUser sharedInstance] cancelAccount:^{
		NSLog(@"cancel account success");
	} failure:^(NSError *error) {
		NSLog(@"cancel account failure: %@", error);
	}];
}

Swift:

func cancelAccount() {
    TuyaSmartUser.sharedInstance()?.cancelAccount({
        print("cancel account success")
    }, failure: { (error) in
        if let e = error {
            print("cancel account failure: \(e)")
        }
    })
}