Manage User Accounts

Last Updated on : 2023-05-22 06:38:27

Modify account information

Change a user avatar

Parameters

Parameter Description
headIcon The image of the avatar.
success The success callback.
failure The failure callback. An error message is returned.

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

Set temperature unit to Celsius or Fahrenheit.

API description

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

Parameters

Parameter Description
tempUnit The temperature unit. Valid values:
  • 1: °C
  • 2: °F
success The success callback.
failure The failure callback. An error message is returned.

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

Modify the nickname

API description

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

Parameters

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

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

Update user’s time zone

API description

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

Parameters

Parameter Description
timeZoneId The time zone ID. Example: Asia/Shanghai.
success The success callback.
failure The failure callback. An error message is returned.

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

Update user’s location

API description

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

Parameters

Parameter Description
latitude The latitude of the user.
longitude The longitude of the user.

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

Log out or switch between accounts

API description

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

Parameters

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

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

Inactivate or delete account

API description

Deletes a user account. During the week following this delete operation, if the user is logged in again, the delete request is canceled. If not, the user is permanently disabled and all its information is deleted after this week.

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

Parameters

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

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

Process session expiration

If users reset the password or have not logged in for a long time, an error message is returned to indicate session expiration. In this case, you must listen for the notifications of TuyaSmartUserNotificationUserSessionInvalid and navigate users to the login page.

ObjC:

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

- (void)sessionInvalid {
		NSLog(@"sessionInvalid");
		// Navigates to the 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")
    // Navigates to the login page.
		let vc = MyLoginViewController()
		window.rootViewController = vc
		window.makeKeyAndVisible()
}