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

Register and Login with Email Address

Last Updated on : 2022-01-04 03:28:50download

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

Send a verification code

API description

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

Parameters

Parameter Description
userName The email address or mobile phone number of the user.
region The area in which an account is registered. You can call [TuyaSmartUser regionListWithCountryCode:success:failure:] or [TuyaSmartUser getDefaultRegionWithCountryCode:] to get the value.
countryCode The country code. For example, 86 means mainland China.
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] sendVerifyCodeWithUserName:@"your_email" region:@"your_region" countryCode:@"your_country_code" type:1 success:^{
    NSLog(@"sendVerifyCode success");
} failure:^(NSError *error) {
    NSLog(@"sendVerifyCode failure: %@", error);
}];

Swift:

TuyaSmartUser.sharedInstance().sendVerifyCode(withUserName: "your_email", region: "your_region", countryCode: "your_country_code", type: 1) {
    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 mobile phone number or email address.
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 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)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 of the user.
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 the email address and password

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 of the user.
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)")
    }
})

Log in to the app with the email address and verification code

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

API description

- (void)loginWithEmail:(NSString *)email
           countryCode:(NSString *)countryCode
                  code:(NSString *)code
               success:(TYSuccessHandler)success
               failure:(TYFailureError)failure;

Parameters

Parameter Description
email The email address.
countryCode The country code.
code The verification code.
success The success callback.
failure The failure callback. An error message is returned.

Example

ObjC:

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

Swift:

TuyaSmartUser.sharedInstance().login(withEmail: "your_email", countryCode: "your_country_code", code: "your_code") {
    print("login success")
} failure: { error in
    if let e = error {
        print("login failure: \(e)")
    }
}

Bind an email address with the account

API description

After users log in to the app with their mobile phone numbers, they can bind email addresses to their accounts. This enables login to the app with the email address. To enable this binding, you must make an API request to send a verification code.

- (void)bindEmail:(NSString *)email
  withCountryCode:(NSString *)countryCode
             code:(NSString *)code
              sId:(NSString *)sId
          success:(nullable TYSuccessHandler)success
          failure:(nullable TYFailureError)failure;

Parameters

Parameter Description
email The email address of the user.
countryCode The country code, such as 86.
code The returned verification code.
sId session Id
success The success callback.
failure The failure callback. An error message is returned.

Example

ObjC:

 [[TuyaSmartUser sharedInstance] sendBindingVerificationCodeWithEmail:userName
                                                          countryCode:countryCode
                                                              success:^{
                                                 // send success
                                                            } failure:^(NSError *error) {

                                                                NSLog(@"sendBindingVerificationCodeWithEmail failure: %@", error);
                                                           }
];

[[TuyaSmartUser sharedInstance] bindEmail:account
                          withCountryCode:countryCode
                                     code:code
                                      sId:[TuyaSmartUser sharedInstance].sid
                                  success:^{
                                                 // bind successd
                                           }];

                                    } failure:^(NSError *error) {

                                          NSLog(@"bind failure: %@", error);
                                    }];

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 of the user.
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)")
        }
    })
}