1. User management

Tuya Cloud supports various kinds of user systems such as mobile phone, email and UID. Mobile phone supports verification code login and password login. The registration and login of each user system will be separately described later.

Class Name Description
TuyaSmartUser User-related classes
TuyaSmartRequest Universal interface classes

The countryCode parameter (country code) needs to be provided in the registration and login method for region selection of Tuya Cloud. Data of all available regions is independent. The Chinese Mainland account (country code: 86) cannot be used in the USA (country code: 1). The Chinese Mainland account does not exist in the USA region.

For details about available region, please refer to the Tuya Cloud-Available Region

All functions of user are realized by using the TuyaSmartUser Class (singleton).

1.1. Use Mobile Phone for Login

Tuya Smart provides the mobile phone verification code login system.

[!DANGER] For the privacy and security of user information, we have optimized the mobile phone number registration mechanism. If you want to use the mobile phone number registration service, you can contact the relevant docking business

1.1.1. Use Mobile Phone Password for Registration

The mobile phone and password registration process is divided into the following two steps: get a mobile phone verification code - Register a mobile phone and password account.

Declaration

Send verification code. Used for mobile phone verification code login, register, password reset.

- (void)sendVerifyCode:(NSString *)countryCode
           phoneNumber:(NSString *)phoneNumber
                  type:(NSInteger)type
               success:(nullable TYSuccessHandler)success
               failure:(nullable TYFailureError)failure;

Parameters

Param Description
countryCode country code, for example: 86
phoneNumber Mobile phone number
type 0: mobile phone verification code login, 1: mobile phone verification code register, 2: mobile phone password reset.
success Success Callback
failure Failure Callback

Example

Objc:

[[TuyaSmartUser sharedInstance] sendVerifyCode:@"your_country_code" phoneNumber:@"your_phone_number" type:1 success:^{
    NSLog(@"sendVerifyCode success");
} failure:^(NSError *error) {
    NSLog(@"sendVerifyCode failure: %@", error);
}];

Swift:

TuyaSmartUser.sharedInstance()?.sendVerifyCode("your_country_code", phoneNumber: "your_phone_number", type: 1, success: {
    print("sendVerifyCode success")
}, failure: { (error) in
    if let e = error {
        print("sendVerifyCode failure: \(e)")
    }
})

Declaration

Register a mobile phone and password account.

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

Parameters

Param Description
countryCode country code, for example: 86
phoneNumber Mobile phone number
password password
code After the verification code sending, the verification code received
success Success Callback
failure Failure Callback

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

1.1.2. Use Mobile Phone Password for Login

Declaration

SDK provides mobile phone number and password login method.

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

Parameters

Param Description
countryCode country code, for example: 86
phoneNumber Mobile phone number
password password
success Success Callback
failure Failure Callback

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

1.1.3. Resetting Password by Using Mobile Phone

The process of resetting the password of the mobile phone number is divided into the following two steps: obtain the mobile phone verification code (the API refers to the first interface of "Use Mobile Phone Password for Registration") - reset the password.

Declaration

Resetting Password by Using Mobile Phone

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

Parameters

Param Description
countryCode country code, for example: 86
phoneNumber Mobile phone number
newPassword new password
code After the verification code sending, the verification code received
success Success Callback
failure Failure Callback

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

[!TIP]

After the password is reset, if multiple devices log in to the same account at the same time, other devices will trigger a callback for session failure. Please implement the actions after the callback, such as jumping to the login page.

For more information, please refer to the chapter "Handling of Expired Session"

1.1.4. Use Mobile Phone Verification Code for Login

The mobile phone number and verification code login process is divided into the following two steps: get the mobile phone verification code (the API refers to the first interface of "Use Mobile Phone Password for Registration") - verification code login.

Declaration

verification code login

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

Parameters

Param Description
mobile Mobile phone number
countryCode country code, for example: 86
code After the verification code sending, the verification code received
success Success Callback
failure Failure Callback

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

1.1.5. Verification code verification

Declaration

Verification verification code, used for verification verification during registration, login, and password reset

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

Parameters

Param Description
userName Phone number or email
region Region, default value is nil
countryCode Country code, for example: 86
code After the verification code sending, the verification code received
type Type, 1: mobile phone verification code register, 2: mobile phone verification code login, 3: mobile phone password reset
success Success Callback
failure Failure Callback

Example

Objc:

[[TuyaSmartUser sharedInstance] checkCodeWithUserName:@"email_or_phone_number" region:@"region" countryCode:@"your_country_code" code:@"verify_code" type:type 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)")
        }
})

1.2. Use Email for Login

Tuya Smart provides the email password login system.

1.2.1. User Email Password Registration

The email registration process is divided into the following two steps: get email verification code - register email password account.

Declaration

send verification code, used for email password register.

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

Parameters

Param Description
countryCode country code, for example: 86
email email
success Success Callback
failure Failure Callback

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

Declaration

Email register

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

Parameters

Param Description
countryCode Country code, for example: 86
email Email
password Password
code After the verification code sending, the verification code received
success Success Callback
failure Failure Callback

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

1.2.2. Use Email Password for Login

Declaration

SDK provides email and password login method.

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

Parameters

Param Description
countryCode Country code, for example: 86
phoneNumber Mobile phone number
password Password
success Success Callback
failure Failure Callback

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

1.2.3. Reset email password

Declaration

The process of resetting the password of the email is divided into the following two steps: obtain the email verification code (the API refers to the first interface of "User Email Password Registration") - reset the password.

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

Parameters

Param Description
countryCode Country code, for example: 86
email Email
newPassword New password
code After the verification code sending, the verification code received
success Success Callback
failure Failure Callback

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

1.2.4. Verification code verification

Declaration

Verification verification code, used for verification verification during registration, login, and password reset

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

Parameters

Param Description
userName Phone number or email
region Region, default value is nil
countryCode Country code, for example: 86
code After the verification code sending, the verification code received
type Type, 1: mobile phone verification code register, 2: mobile phone verification code login, 3: mobile phone password reset
success Success Callback
failure Failure Callback

Example

Objc:

[[TuyaSmartUser sharedInstance] checkCodeWithUserName:@"email_or_phone_number" region:@"region" countryCode:@"your_country_code" code:@"verify_code" type:type 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)")
        }
})

1.3. Use Uid for Login

1.3.1. User Uid Registration and Login

Declaration

If had registered, then automatically logged in. If had not registered, then automatically registered and logged in.

- (void)loginOrRegisterWithCountryCode:(NSString *)countryCode
                                   uid:(NSString *)uid
                              password:(NSString *)password
                            createHome:(BOOL)createHome
                               success:(nullable TYSuccessID)success
                               failure:(nullable TYFailureError)failure;

Parameters

Param Description
countryCode Country code, for example: 86
uid user id
password Password
createHome Whether to create a default family
success Success Callback
failure Failure Callback

Example

Objc:

[[TuyaSmartUser sharedInstance] loginOrRegisterWithCountryCode:@"your_country_code" uid:@"your_uid" password:@"your_password" createHome:YES success:^(id result) {
        NSLog(@"loginOrRegisterWithCountryCode success: %@", result);
} failure:^(NSError *error) {
        NSLog(@"loginOrRegisterWithCountryCode failure: %@", error);
}];

Swift:

TuyaSmartUser.sharedInstance()?.loginOrRegisterWithCountryCode("your_country_code", uid: "your_uid", password: "your_password", createHome: true, success: { (result) in 
        print("loginOrRegisterWithCountryCode success: \(result)")
}, failure: { (error) in 
        if let e = error {
            print("loginOrRegisterWithCountryCode failure: \(e)")
    }
})

1.4. Third Party Login

User needs to configure corresponding AppID and AppSecret in the Tuya developer platformApp developmentThird-party login. The client shall be developed according to requirements of all platforms. After corresponding code is obtained, relevant login interface of tuyaSDK shall be invoked.

1.4.1. Login on Wechat

Declaration

Login on Wechat

- (void)loginByWechat:(NSString *)countryCode
                 code:(NSString *)code
              success:(nullable TYSuccessHandler)success
              failure:(nullable TYFailureError)failure;

Parameters

Param Description
countryCode Country code, for example: 86
code Code from Wechat authorization login
success Success Callback
failure Failure Callback

Example

Objc:

- (void)loginByWechat {
    [[TuyaSmartUser sharedInstance] loginByWechat:@"your_country_code" code:@"wechat_code" success:^{
        NSLog(@"login success");
    } failure:^(NSError *error) {
        NSLog(@"login failure: %@", error);
    }];
}

Swift:

func loginByWechat() {
    TuyaSmartUser.sharedInstance()?.login(byWechat: "your_country_code", code: "wechat_code", success: {
        print("login success")
    }, failure: { (error) in
        if let e = error {
            print("login failure: \(e)")
        }
    })
}

1.4.2. Login on QQ

Declaration

Login on QQ

- (void)loginByQQ:(NSString *)countryCode
           userId:(NSString *)userId
      accessToken:(NSString *)accessToken
          success:(nullable TYSuccessHandler)success
          failure:(nullable TYFailureError)failure;

Parameters

Param Description
countryCode Country code, for example: 86
userId User id
accessToken AccessToken from QQ authorization login
success Success Callback
failure Failure Callback

Example

Objc:

- (void)loginByQQ {
    [[TuyaSmartUser sharedInstance] loginByQQ:@"your_country_code" openId:@"qq_open_id" accessToken:@"access_token" success:^{
        NSLog(@"login success");
    } failure:^(NSError *error) {
        NSLog(@"login failure: %@", error);
    }];

}

Swift:

 func loginByQQ() {
    TuyaSmartUser.sharedInstance()?.login(byQQ: "your_country_code", userId: "qq_open_id", accessToken: "access_token", success: {
        print("login success")
    }, failure: { (error) in
        if let e = error {
            print("login failure: \(e)")
        }
    })
}

1.4.3. Login on Facebook

Declaration

Login on Facebook

- (void)loginByFacebook:(NSString *)countryCode
                  token:(NSString *)token
                success:(nullable TYSuccessHandler)success
                failure:(nullable TYFailureError)failure;

Parameters

Param Description
countryCode Country code, for example: 86
token Token from Facebook authorization login
success Success Callback
failure Failure Callback

Example

Objc:

- (void)loginByFacebook {
    [[TuyaSmartUser sharedInstance] loginByFacebook:@"your_country_code" token:@"facebook_token" success:^{
        NSLog(@"login success");
    } failure:^(NSError *error) {
        NSLog(@"login failure: %@", error);
    }];
}

Swift:

 func loginByFacebook() {
    TuyaSmartUser.sharedInstance()?.login(byFacebook: "your_country_code", token: "facebook_token", success: {
        print("login success")
    }, failure: { (error) in
        if let e = error {
            print("login failure: \(e)")
        }
    })
}

1.4.4. Login by Twitter

Declaration

Login by Twitter

- (void)loginByTwitter:(NSString *)countryCode
                   key:(NSString *)key
                secret:(NSString *)secret
               success:(nullable TYSuccessHandler)success
               failure:(nullable TYFailureError)failure;

Parameters

Param Description
countryCode Country code, for example: 86
key Key from Twitter authorization login
secret ecret from Twitter authorization login
success Success Callback
failure Failure Callback

Example

Objc:


- (void)loginByTwitter {
    [[TuyaSmartUser sharedInstance] loginByTwitter:@"your_country_code" key:@"twitter_key" secret:@"twitter_secret" success:^{
        NSLog(@"login success");
    } failure:^(NSError *error) {
        NSLog(@"login failure: %@", error);
    }];

}

Swift:

func loginByTwitter() {
    TuyaSmartUser.sharedInstance()?.login(byTwitter: "your_country_code", key: "twitter_key", secret: "twitter_secret", success: {
           print("login success")
    }, failure: { (error) in
        if let e = error {
            print("login failure: \(e)")
        }
    })
}

1.5. Login by Auth2

Declaration

Auth2 is a general login interface. You can use some auth2 login type by passed type parameters.

- (void)loginByAuth2WithType:(NSString *)type
                 countryCode:(NSString *)countryCode
                 accessToken:(NSString *)accessToken
                   extraInfo:(NSDictionary *)extraInfo
                     success:(nullable TYSuccessHandler)success
                     failure:(nullable TYFailureError)failure;

Parameters

Param Description
type type of Auth2 login(@"ap" for login with apple)
countryCode Country code, for example: 86
accessToken AccessToken
extraInfo Extra info
success Success Callback
failure Failure Callback

Example

Objc:

- (void)loginWithAuth2 {
    [[TuyaSmartUser sharedInstance] loginByAuth2WithType:@"auth2_type" countryCode:@"your_country_code" accessToken:@"auth2_token" extraInfo:@{@"info_key": @"info_value"} success:^{
        NSLog(@"login success");
    } failure:^(NSError *error) {
        NSLog(@"login failure: %@", error);
    }];
}

Swift:

func loginWithAuth2() {
    TuyaSmartUser.sharedInstance().loginByAuth2WithType("auth2_type", countryCode: "your_country_code", accessToken: "auth2_token", extraInfo: ["info_key":"info_value"], success: {
        print("login success")
    }, failure: { (error) in
        if let e = error {
                print("login failure: \(e)")
        }
    })
}

1.5.1. Login with Apple

Declaration

The SDK supports Login with Apple from 3.14.0. After Apple login authorization is successful, information such as token and extraInfo are passed to the SDK through the Auth2 interface.

Parameters

Param Description
type @"ap"
countryCode Country code, for example: 86
accessToken credential.identityToken
extraInfo @{@"userIdentifier": credential.user, @"email": credential.email, @"nickname":credential.fullName.nickname, @"snsNickname": credential.fullName.nickname}
success Success Callback
failure Failure Callback

Example

Objc:

- (void)loginWithApple {
    ASAuthorizationAppleIDCredential *credential = authorization.credential;

    [[TuyaSmartUser sharedInstance] loginByAuth2WithType:@"ap" countryCode:@"your_country_code" accessToken:credential.identityToken extraInfo:{@"userIdentifier": credential.user, @"email": credential.email, @"nickname": credential.fullName.nickname, @"snsNickname": credential.fullName.nickname} success:^{
        NSLog(@"login success");
    } failure:^(NSError *error) {
        NSLog(@"login failure: %@", error);
    }];
}

Swift:

func loginWithApple() {
    let credential = authorization.credential
    TuyaSmartUser.sharedInstance().loginByAuth2(withType: "ap", countryCode: "your_country_code", accessToken: credential?.identityToken, extraInfo:     ["userIdentifier": user,"email": email,"nickname": nickname,"snsNickname": nickname], success: {
        print("login success")
    }, failure: { (error) in
        if let e = error {
                print("login failure: \(e)")
        }
    })
}

1.5.2. Login with Google

Declaration

The SDK supports Google login from 3.19.0. After authorization is successful, pass in token (here is google idToken) and extraInfo information through the Auth2 interface to achieve Google login.

Parameters

Param Description
type @"gg"
countryCode Country code, for example: 86
accessToken google authentication idToken ,google authorized id token
extraInfo @{@"pubVersion": @1}
success Success Callback
failure Failure Callback

Example

Objc:


- (void)loginWithGoogle {

    NSString *loginType = @"gg";    //google login
    NSString *countryCode = @"1";   //USA
    NSString *accessToken = @"id token from google";  // google authorized id token

    [[TuyaSmartUser sharedInstance] loginByAuth2WithType:@"gg" 
                                             countryCode:countryCode 
                                             accessToken:accessToken 
                                                extraInfo:@{@"pubVersion": @1}    
    success:^{
        NSLog(@"login success");
    } failure:^(NSError *error) {
        NSLog(@"login failure: %@", error);
    }];
}

Swift:

func loginWithGoogle() {

    let loginType = "gg" //google login
    let countryCode = "1" //USA
    let accessToken = "id token from google" // google authorized id token

    TuyaSmartUser.sharedInstance().loginByAuth2(
        withType: "gg",
        countryCode: countryCode,
        accessToken: accessToken,
        extraInfo: [
            "pubVersion": NSNumber(value: 1)
        ],
        success: {
            print("login success")
        },
        failure: { error in
            if let error = error {
                print("login failure: \(error)")
            }
        })
}

1.6. Anonymous registration

1.6.1. Anonymous registration

Declaration

SDK provides anonymous registration to log in, passing parameters: usename, anonymous login nickname; countryCode, country code.

- (void)registerAnonymousWithCountryCode:(NSString *)countryCode
                                userName:(NSString *)userName
                                 success:(TYSuccessHandler)success
                                 failure:(TYFailureError)failure;

Parameters

Params Type Description
countryCode NSString* Country code, 86: China, 1: USA
userName NSString* Nickname of anonymous login (for example: device name)
success TYSuccessHandler Success Callback
failure TYFailureError Failure Callback

Example

Objc:


NSString *countryCode = @"1"; // USA
NSString *usename = [UIDevice currentDevice].name;  // Device name
[[TuyaSmartUser sharedInstance] registerAnonymousWithCountryCode:countryCode 
                                                        userName:usename
                                                         success:^{
    NSLog(@"anonymouse success");
} failure:^(NSError *error) {
    NSLog(@"anonymouse failure: %@", error);
}];

Swift:

let countryCode = "1" // USA
let usename = UIDevice.current.name // Device name
TuyaSmartUser.sharedInstance().registerAnonymous(withCountryCode: countryCode, 
                                                        userName: usename, 
                                                        success: {
    print("anonymouse success")
}, failure: { error in
    if let error = error {
        print("anonymouse failure: \(error)")
    }
})

1.6.2. Anonymous user logout

Declaration

Users who log in anonymously can log out through this interface. Anonymous accounts will be logged out immediately.

- (void)deleteAnonymousAccountWithSuccess:(TYSuccessHandler)success
                                  failure:(TYFailureError)failure

Parameters

Params Type Description
success TYSuccessHandler Success Callback
failure TYFailureError Failure Callback

Example

Objc:


[[TuyaSmartUser sharedInstance] deleteAnonymousAccountWithSuccess:^{
    NSLog(@"anonymous logout success");
} failure:^(NSError *error) {
    NSLog(@"anonymous logout failure: %@", error);
}];

Swift:


TuyaSmartUser.sharedInstance().deleteAnonymousAccount(withSuccess: {
    print("anonymous logout success")
}, failure: { error in
    if let error = error {
        print("anonymous logout failure: \(error)")
    }
})

1.6.3. Anonymous user bind account

Declaration

Users who log in anonymously can further improve their mobile phone or email information and transform them into normal users. There are usually two steps to perfecting information:

  • Verify email or mobile phone
  • Set account password
- (void)usernameBindingWithCountryCode:(NSString *)countryCode
                              userName:(NSString *)userName
                                  code:(NSString *)code
                              password:(NSString *)password
                               success:(nullable TYSuccessHandler)success
                               failure:(nullable TYFailureError)failure;

Parameters

Params Type Description
countryCode NSString* Country code(For example: 1, USA; 86, China)
userName NSString* User's phone number or email
code NSString* Verification code
password NSString* Password
success TYSuccessHandler Success Callback
failure TYFailureError Failure Callback

Example

Objc:

NSString *countryCode = @"1"; // USA
NSString *username = @"example@test.com";   // User's phone number or email
NSString *code = @"verify code numbers";    // Verification code
NSString *password = @"account password";   // Password
[[TuyaSmartUser sharedInstance] usernameBindingWithCountryCode:countryCode
                                                          userName:username
                                                              code:code
                                                          password:password
                                                           success:^{
    NSLog(@"username bind success");
} failure:^(NSError *error) {
    NSLog(@"username bind failure:%@", error);
}];

Swift:


let countryCode = "1" // USA
let username = "example@test.com" // User's phone number or email
let code = "verify code numbers" // Verification code
let password = "account password" // Password
TuyaSmartUser.sharedInstance().usernameBinding(withCountryCode: countryCode, userName: username, code: code, password: password, success: {
    print("username bind success")
}, failure: { error in
    if let error = error {
        print("username bind failure:\(error)")
    }
})

1.7. Account Binding

1.7.1. User Email Binding

The email binding process is divided into the following two steps: get email verification code - Bind the email to the currently logged in user account.

Declaration

send verification code, used for email binding.

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

Parameters

Param Description
email Email
countryCode Country code, for example: 86
success Success Callback
failure Failure Callback

Example

Objc:

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

Swift:

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

Declaration

Bind email to current login account.

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

Parameters

Param Description
email Email
countryCode Country code, for example: 86
code After the verification code sending, the verification code received
sId Session ID of the user after login
success Success Callback
failure Failure Callback

Example

Objc:

[[TuyaSmartUser sharedInstance] bindEmail:@"your_email" withCountryCode:@"your_country_code" code:@"verify_code" sId:@"session_id" success:^{
    NSLog(@"register success");
} failure:^(NSError *error) {
    NSLog(@"register failure: %@", error);
}];

Swift:

TuyaSmartUser.sharedInstance()?.bind(email: "your_email", withCountryCode: "your_country_code", code: "verify_code", sId: "session_id", success: {
    print("register success")
}, failure: { (error) in
    if let e = error {
        print("register failure: \(e)")
    }
})

1.8. Modify User Info

1.8.1. Modify User Avatar

Interface description

Declaration

Used to upload user-defined avatars.

Parameters

Param Description
headIcon Head Icon Image
success Success Callback
failure Failure Callback

Example

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

1.8.2. Set the Unit of Temperature

Declaration

Set the temperature in Celsius or Fahrenheit

- (void)updateTempUnitWithTempUnit:(NSInteger)tempUnit
                           success:(nullable TYSuccessHandler)success
                           failure:(nullable TYFailureError)failure;

Parameters

Param Description
tempUnit Temperature unit. 1 for °C, 2 for °F
success Success Callback
failure Failure Callback

Example

- (void)updateTempUnitWithTempUnit:(NSInteger)tempUnit {
    [[TuyaSmartUser sharedInstance] updateTempUnitWithTempUnit:tempUnit success:^{
        NSLog(@"update temp unit success");
    } failure:^(NSError *error) {
        NSLog(@"update temp unit failure: %@", error);
    }];
}

Swift:

func updateTempUnit(withTempUnit tempUnit: Int) {
    TuyaSmartUser.sharedInstance().updateTempUnit(withTempUnit: tempUnit, success: {
        print("update temp unit success")
    }, failure: { error in
        if let error = error {
            print("update temp unit failure: \(error)")
        }
    })
}

1.8.3. Modify Nickname

Declaration

Modify nickname

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

Parameters

Param Description
nickName Nick name
success Success Callback
failure Failure Callback

Example

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

1.8.4. Update Timezone

Declaration

Update timezone

- (void)updateTimeZoneWithTimeZoneId:(NSString *)timeZoneId
                             success:(nullable TYSuccessHandler)success
                             failure:(nullable TYFailureError)failure;

Parameters

Param Description
timeZoneId TimeZone ID. e.g. Asia/Shanghai.
success Success Callback
failure Failure Callback

Example

Objc:

- (void)updateTimeZoneId:(NSString *)timeZoneId {
    [[TuyaSmartUser sharedInstance] updateTimeZoneWithTimeZoneId:timeZoneId success:^{
        NSLog(@"update timeZoneId success");
    } failure:^(NSError *error) {
        NSLog(@"update timeZoneId failure: %@", error);
    }];
}

Swift:

func updateTimeZoneId(_ timeZoneId: String) {
    TuyaSmartUser.sharedInstance()?.updateTimeZone(withTimeZoneId: timeZoneId, success: {
        print("update timeZoneId success")
    }, failure: { (error) in
        if let e = error {
            print("update timeZoneId failure: \(e)")
        }
    })
}

1.8.5. Update Location

Declaration

If need, location can be reported through this api:

- (void)updateLatitude:(double)latitude longitude:(double)longitude;

Parameters

Param Description
latitude latitude
longitude longitude

Example

Objc:

- (void)updateLocation {
  double latitude = 30.000;
  double longitude = 120.000;
  [[TuyaSmartSDK sharedInstance] updateLatitude:latitude longitude:longitude];
}

Swift:

func updateLocation() {
  TuyaSmartSDK.sharedInstance()?.updateLatitude(latitude, longitude: longitude);
}

1.9. Logout, Disable Account

1.9.1. Logout

Declaration

When the user account is switched, it is necessary to call the logout interface.

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

Parameters

Param Description
success Success Callback
failure Failure Callback

Example

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

1.9.2. Disable Account (Deregister User)

Declaration

After one week, the account will be permanently disabled, and all information in the account will be deleted. If you log in to the account again before it is permanently disabled, your deregistration will be canceled.

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

Parameters

Param Description
success Success Callback
failure Failure Callback

Example

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

1.10. User Data Model

TuyaSmartUser:

User

Parameters Description
nickName Nickname
countryCode Country code
phoneNumber Mobile phone number
userName User name
email Email
uid Unique user identifier
sid Login generates the unique identification id
headIconUrl Url of the User Account Picture

1.11. Handling of Expired Session

If you have not logged in to your account for a long time, the session expiration error will be returned when you access the server interface. You have to monitor the notification of the TuyaSmartUserNotificationUserSessionInvalid and log in to the account again after the login page is displayed.

Objc:


- (void)loadNotification {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sessionInvalid) name:TuyaSmartUserNotificationUserSessionInvalid object:nil];
}

- (void)sessionInvalid {
  NSLog(@"sessionInvalid");
  // Go to login page
    MyLoginViewController *vc = [[MyLoginViewController alloc] init];
    self.window.rootViewController = vc;
    [self.window makeKeyAndVisible];
}

Swift:

func loadNotification() {
    NotificationCenter.default.addObserver(self, selector: #selector(sessionInvalid), name: NSNotification.Name(rawValue: "TuyaSmartUserNotificationUserSessionInvalid"), object: nil)
}

@objc func sessionInvalid() {
    print("sessionInvalid")
    // Go to login page
        let vc = MyLoginViewController()
        window.rootViewController = vc
        window.makeKeyAndVisible()
}

results matching ""

    No results matching ""