English
English
简体中文
Contact Us
Register
Log In
layoutIndex

Register and Login to an Account with an Email Address

Last Updated on : 2022-03-03 06:41:03download

Tuya Smart Residence App SDK supports login with a verification code that is sent to a user’s email address.

Send a verification code

API description

- (void)sendVerifyCodeByRegisterEmail:(NSString *)countryCode
                                email:(NSString *)email
                              success:(nullable TYSuccessHandler)success
                              failure:(nullable TYFailureError)failure;

Parameters

Parameter Description
countryCode The country code, such as 86.
email The email address.
success The success callback.
failure The failure callback. An error message is returned.

Example

ObjC:

[[TuyaSmartUser sharedInstance] sendVerifyCodeByRegisterEmail:@"your_country_code" email:@"your_email" success:^{
  	NSLog(@"sendVerifyCode success");
} failure:^(NSError *error) {
    NSLog(@"sendVerifyCode failure: %@", error);
}];

Swift:

TuyaSmartUser.sharedInstance()?.sendVerifyCode(byRegisterEmail: "your_country_code", email: "your_email", success: {
    print("sendVerifyCode success")
}, failure: { (error) in
    if let e = error {
        print("sendVerifyCode failure: \(e)")
    }
})

Verify a verification code

API description

Verifies a verification code that is used for login, registration, or password resetting with an email address.

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

Parameters

Parameter Description
userName The email address of the user.
region The area in which the account is registered. Default value: nil.
countryCode The country code, such as 86.
code The returned verification code.
type The purpose of the verification code. Valid values:
  • 1: register an account with an email address
  • 2: login to the app with an email address
  • 3: reset the password of an account that is registered with an email address
success The success callback.
failure The failure callback. An error message is returned.

Example

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)")
		}
})

Register an account with an email address

To allow users to register an account with an email address, you must make an API request to send a verification code.

API description

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

Parameters

Parameter Description
countryCode The country code, such as 86.
email The email address.
password The password.
code The returned verification code.
success The success callback.
failure The failure callback. An error message is returned.

Example

ObjC:

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

Swift:

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

Login to the account with an email address

API description

- (void)loginByEmail:(NSString *)countryCode
               email:(NSString *)email
            password:(NSString *)password
             success:(nullable TYSuccessHandler)success
             failure:(nullable TYFailureError)failure;

Parameters

Parameter Description
countryCode The country code, such as 86.
email The email address used for login.
password The password.
success The success callback.
failure The failure callback. An error message is returned.

Example

ObjC:

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

Swift:

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

Reset account password

To allow users to reset the password of an account that is registered with an email address, you must make an API request to send a verification code.

API description

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

Parameters

Parameter Description
countryCode The country code, such as 86.
email The email address.
newPassword The new password.
code The returned verification code.
success The success callback.
failure The failure callback. An error message is returned.

Example

ObjC:

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

Swift:

func resetPasswordByEmail() {
    TuyaSmartUser.sharedInstance()?.resetPassword(byEmail: "your_country_code", email: "your_email", newPassword: "your_password", code: "verify_code", success: {
        print("resetPasswordByEmail success")
    }, failure: { (error) in
        if let e = error {
            print("resetPasswordByEmail failure: \(e)")
        }
    })
}