English
English
简体中文
Contact Us
Register
Log In
Language
English
English
简体中文
Contact Us
Log In
Register
Go to main website
App Develop

App Development

Tuya provides multiple mobile app development methods such as no-code or IoT App SDK development to maximize the monetization of IoT apps.
layoutIndex

Register and Login to Account with Phone Number

Last Updated on : 2021-11-19 01:43:08download

Tuya Smart Life App SDK supports registration and login with a user’s mobile phone number and password.

Query areas for verification code service

To strengthen data security, Tuya has optimized the verification code service and placed limits on accounts. The verification code service is available only in limited areas. If you want to launch your application in other countries or areas, you must verify that the verification code service is available in a specific country or area and contact your account manager of Tuya or submit a ticket to activate the service.

- (void)getWhiteListWhoCanSendMobileCodeSuccess:(TYSuccessString)success
                                        failure:(TYFailureError)failure;

Parameters

Parameter Description
success The success callback.
failure The failure callback. An error message is returned.

ObjC:

[[TuyaSmartUser sharedInstance] getWhiteListWhoCanSendMobileCodeSuccess:^(NSString *regions) {

} failure:^(NSError *error) {

}];

Swift:

TuyaSmartUser.sharedInstance().getWhiteListWhoCanSendMobileCodeSuccess({ regions in

}, failure: { error in

})

Send verification code

Make sure that you can call this API method. You can call getWhiteListWhoCanSendMobileCodeSuccess to check your permissions.

API description

Sends a verification code that is used for login, registration, or password resetting with a mobile phone number.

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

Parameters

Parameter Description
userName The mobile phone number.
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, such as 86.
type The purpose of the verification code. Valid values:
  • 1: register an account with a mobile phone number
  • 2: login to the app with a mobile phone number
  • 3: reset the password of an account that is registered with a mobile phone number
success The success callback.
failure The failure callback. An error message is returned.

Example

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

Verify verification code

API description

Verifies a verification code that is used for login, registration, or password resetting with a mobile phone number.

- (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 used as the username.
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 a mobile phone number
  • 2: login to the app with a mobile phone number
  • 3: reset the password of an account that is registered with a mobile phone number
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 account with mobile phone number

To allow users to register an account with a mobile phone number, you must make an API request to send a verification code.

API description

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

Parameters

Parameter Description
countryCode The country code, such as 86.
phoneNumber The mobile phone number.
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] 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)")
    }
})

Log in to app with phone number and password

API description

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

Parameters

Parameter Description
countryCode The country code, such as 86.
phoneNumber The mobile phone number.
password The password.
success The success callback.
failure The failure callback. An error message is returned.

Example

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

Log in to app with phone number and verification code

To enable login with a mobile phone number and a verification code, you must make an API request to send a verification code to the mobile phone for login and then make an API request to verify the entered verification code.

API description

- (void)loginWithMobile:(NSString *)mobile
            countryCode:(NSString *)countryCode
                   code:(NSString *)code
                success:(TYSuccessHandler)success
                failure:(TYFailureError)failure;

Parameters

Parameter Description
mobile The mobile phone number.
countryCode The country code, such as 86.
code The returned verification code.
success The success callback.
failure The failure callback. An error message is returned.

Example

ObjC:

[[TuyaSmartUser sharedInstance] loginWithMobile:@"your_phone_number" countryCode:@"your_country_code" code:@"verify_code" success:^{
		NSLog(@"login success");
} failure:^(NSError *error) {
    NSLog(@"login failure: %@", error);
}];

Swift:

TuyaSmartUser.sharedInstance()?.login(withMobile: "your_phone_number", countryCode: "your_country_code", code: "verify_code", 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 a mobile phone number, you must make an API request to send a verification code.

API description

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

Parameters

Parameter Description
countryCode The country code, such as 86.
phoneNumber The mobile phone number.
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)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)")
        }
    })
}

If an account is logged in on different mobile phones, after the password is reset with one of the phones, the app on other phones triggers the callback of session expiration. You must implement the actions after the callback. For example, navigate to the login page. For more information, see Process session expiration.