Device Sharing

Last Updated on : 2023-05-25 06:23:48

A user can share a device in a home with other users in the same home so that the other users can control the device. The users that receive a sharing invitation can control the device by using their app account.

Each device can be simultaneously shared with up to 20 users.

Integrate the class

Class name Description
TuyaSmartHomeDeviceShare Provides the capabilities to share devices.

ObjC:

#import <TuyaSmartDeviceKit/TuyaSmartHomeDeviceShare.h>

Swift:

import TuyaSmartDeviceKit

Share devices

Share one or more devices

Shares one or more devices with a specific user. Each device can be simultaneously shared with up to 20 users.

The API methods addShareWithHomeId:countryCode:userAccount:devIds:failure: and addDeviceShareWithHomeId:countryCode:userAccount:devId:success:failure: will soon be deprecated. We recommend that you timely update the API.

API description

- (void)addDeviceShareWithRequestModel:(TuyaSmartDeviceShareRequestModel *)requestModel
                            success:(void(^)(TuyaSmartShareMemberModel *model))success
                            failure:(TYFailureError)failure;

Parameters

Parameter Description
requestModel The request object.
success The success callback.
failure The failure callback.

TuyaSmartDeviceShareRequestModel

@interface TuyaSmartDeviceShareRequestModel : NSObject

/// The ID of the home to which the shared devices belong.
@property (nonatomic, assign) long long homeID;

/// The country code of the user that receives a sharing invitation.
@property (nonatomic, copy) NSString *countryCode;

/// The account of the user that receives a sharing invitation.
@property (nonatomic, copy) NSString *userAccount;

/// The list of device IDs for sharing.
@property (nonatomic, copy) NSArray<NSString *> *devIds;

@end

Example

ObjC:

- (void)addMemberShare {
//self.deviceShare  = [[TuyaSmartHomeDeviceShare alloc] init];

TuyaSmartDeviceShareRequestModel *model = [TuyaSmartDeviceShareRequestModel new];
model.homeID = Home ID;
model.countryCode = Country code;
model.userAccount = Target user account;
model.devIds = List of shared device IDs;

[self.deviceShare addDeviceShareWithRequestModel:model success:^(TuyaSmartShareMemberModel * _Nonnull model) {
    //do something
} failure:^(NSError *error) {
    //do something
}];
}

Swift:

func addMemberShare() {
var model = TuyaSmartDeviceShareRequestModel.init()
model.homeID = Home ID
model.countryCode =
model.userAccount = Target user account
model.devIds = List of shared device IDs

deviceShare.add(with: model) { (_) in
    //do something
} failure: { (_) in
    //do something
}
}

Share devices

Shares multiple devices to a specific user and adds these devices following earlier sharing with the user. Each device can be simultaneously shared with up to 20 users.

API description

- (void)addShareWithMemberId:(NSInteger)memberId
                    devIds:(NSArray <NSString *> *)devIds
                    success:(TYSuccessHandler)success
                    failure:(TYFailureError)failure;

Parameters

Parameter Description
memberId The ID of the user that receives a sharing invitation.
success The success callback.
failure The failure callback.

Example

ObjC:

- (void)addMemberShare {
//self.deviceShare  = [[TuyaSmartHomeDeviceShare alloc] init];

[self.deviceShare addShareWithMemberId:memberId devIds:(NSArray<NSString *> *) success:^{
    NSLog(@"addShare success");
} failure:^(NSError *error) {
    NSLog(@"addShare failure: %@", error);
}];
}

Swift:

func addMemberShare() {
deviceShare?.add(withMemberId: memberId, devIds: ["devIds"], success: {
    print("addShare success")
}, failure: { (error) in
    if let e = error {
        print("addShare failure: \(e)")
    }
})
}

[Legacy] Share multiple devices and overwrite earlier sharing

Shares multiple devices to a specific user and overwrite earlier sharing with the user.

This API method will soon be deprecated. Replace it with addDeviceShareWithRequestModel:success:failure:.

API description

-(void)addShareWithHomeId:homeId
            countryCode:countryCode
            userAccount:userAccount
                devIds:(NSArray<NSString *> *) success
                failure:(nullable TYFailureError)failure;

Parameters

Parameter Description
homeId The ID of the home to which the devices belong.
countryCode The country code of the user that receives a sharing invitation.
userAccount The account of the user that receives a sharing invitation.
devIds The list of device IDs for sharing.
success The success callback.
failure The failure callback.

Example

ObjC:

- (void)addMemberShare {
//self.deviceShare  = [[TuyaSmartHomeDeviceShare alloc] init];

[self.deviceShare addShareWithHomeId:homeId countryCode:@"your_country_code" userAccount:@"user_account" devIds:(NSArray<NSString *> *) success:^(TuyaSmartShareMemberModel *model) {
    NSLog(@"addShare success");
} failure:^(NSError *error) {
    NSLog(@"addShare failure: %@", error);
}];
}

Swift:

func addMemberShare() {
deviceShare?.add(withHomeId: homeId, countryCode: "your_country_code", userAccount: "user_account", devIds: ["devIds"], success: { (memberModel) in
    print("addShare success")
}, failure: { (error) in
    if let e = error {
        print("addShare failure: \(e)")
    }
})
}

[Legacy] Share a device

Shares a single device to a specific user and adds the devices following earlier sharing with the user. Each device can be simultaneously shared with up to 20 users.

This API method will soon be deprecated. Replace it with addDeviceShareWithRequestModel:success:failure:.

API description

- (void)addDeviceShareWithHomeId:(long long)homeId
                    countryCode:(NSString *)countryCode
                    userAccount:(NSString *)userAccount
                        devId:(NSString *)devId
                        success:(void(^)(TuyaSmartShareMemberModel *model))success
                        failure:(TYFailureError)failure;

Parameters

Parameter Description
homeId The ID of the home to which the device belongs.
countryCode The country code of the user that receives a sharing invitation.
userAccount The account of the user that receives a sharing invitation.
devId The ID of the device to be shared.
success The success callback.
failure The failure callback.

Example

ObjC:

- (void)addDeviceShare {
    //self.deviceShare  = [[TuyaSmartHomeDeviceShare alloc] init];

[self.deviceShare addDeviceShareWithHomeId:homeId countryCode:@"country_code" userAccount:@"user_account" devId:@"dev_id" success:^(TuyaSmartShareMemberModel *model) {

    NSLog(@"addDeviceShare success");

} failure:^(NSError *error) {

    NSLog(@"addDeviceShare failure: %@", error);

}];

}

Swift:

func addDeviceShare() {
deviceShare?.add(withHomeId: homeId, countryCode: "country_code", userAccount: "user_account", devId: "dev_id", success: { (model) in
    print("addDeviceShare success")
}, failure: { (error) in
    if let e = error {
        print("addDeviceShare failure: \(e)")
    }
})
}

Query sharing relationships

Query all users that initiate sharing invitations in a home

API description

- (void)getReceiveMemberListWithSuccess:(void(^)(NSArray<TuyaSmartShareMemberModel *> *list))success
                            failure:(TYFailureError)failure;

Parameters

Parameter Description
success The success callback.
failure The failure callback.

Example

ObjC:

- (void)getReceiveMemberList {
//self.deviceShare  = [[TuyaSmartHomeDeviceShare alloc] init];

[self.deviceShare getReceiveMemberListWithSuccess:^(NSArray<TuyaSmartShareMemberModel *> *list) {

    NSLog(@"getReceiveMemberList success");

} failure:^(NSError *error) {

    NSLog(@"getReceiveMemberList failure: %@", error);

}];
}

Swift:

func getReceiveMemberList() {
deviceShare?.getReceiveMemberList(success: { (list) in
    print("getReceiveMemberList success")
}, failure: { (error) in
    if let e = error {
        print("getReceiveMemberList failure: \(e)")
    }
})
}

[Legacy] Query all users that initiate sharing invitations in a home

The API method getShareMemberListWithHomeId:success:failure: will soon be deprecated. We recommend that you timely update the API.

API description

- (void)getShareMemberListWithSpaceId:(long long)spaceId
                            success:(void(^)(NSArray<TuyaSmartShareMemberModel *> *list))success
                            failure:(TYFailureError)failure;

Parameters

Parameter Description
spaceId The ID of a specified space.
success The success callback.
failure The failure callback.

Example

ObjC:

- (void)getShareMemberList {
//self.deviceShare  = [[TuyaSmartHomeDeviceShare alloc] init];

[self.deviceShare getShareMemberListWithSpaceId:homeId success:^(NSArray<TuyaSmartShareMemberModel *> *list)

    NSLog(@"getShareMemberList success");

} failure:^(NSError *error) {

    NSLog(@"getShareMemberList failure: %@", error);

}];

}

Swift:

func getShareMemberList() {
deviceShare?.getMemberList(withSpaceId: homeId, success: { (list) in
    print("getShareMemberList success")
}, failure: { (error) in
    if let e = error {
        print("getShareMemberList failure: \(e)")
    }
})
}

Query all users that receive sharing invitations in a home

The API method will soon be deprecated. Replace it with getShareMemberListWithSpaceId:success:failure:.

API description

- (void)getShareMemberListWithHomeId:(long long)homeId
                            success:(void(^)(NSArray<TuyaSmartShareMemberModel *> *list))success
                            failure:(TYFailureError)failure;

Parameters

Parameter Description
homeId
success The success callback.
failure The failure callback.

Example

ObjC:

- (void)getShareMemberList {
//self.deviceShare  = [[TuyaSmartHomeDeviceShare alloc] init];

[self.deviceShare getShareMemberListWithHomeId:homeId success:^(NSArray<TuyaSmartShareMemberModel *> *list)

    NSLog(@"getShareMemberList success");

} failure:^(NSError *error) {

    NSLog(@"getShareMemberList failure: %@", error);

}];

}

Swift:

func getShareMemberList() {
deviceShare?.getMemberList(withHomeId: homeId, success: { (list) in
    print("getShareMemberList success")
}, failure: { (error) in
    if let e = error {
        print("getShareMemberList failure: \(e)")
    }
})
}

Query sharing details of a user that initiates a sharing invitation

API description

- (void)getShareMemberDetailWithMemberId:(NSInteger)memberId
                                success:(void(^)(TuyaSmartShareMemberDetailModel *model))success
                                failure:(TYFailureError)failure;

Parameters

Parameter Description
memberId The ID of the user that initiates the sharing invitations.
success The success callback.
failure The failure callback.

Example

ObjC:

- (void)getShareMemberDetail {
    //self.deviceShare  = [[TuyaSmartHomeDeviceShare alloc] init];

[self.deviceShare getShareMemberDetailWithMemberId:memberId success:^(TuyaSmartShareMemberDetailModel *model) {

    NSLog(@"getShareMemberDetail success");

} failure:^(NSError *error) {

    NSLog(@"getShareMemberDetail failure: %@", error);

}];

}

Swift:

func getShareMemberDetail() {
deviceShare?.getMemberDetail(withMemberId: memberId, success: { (model) in
    print("getShareMemberDetail success")
}, failure: { (error) in
    if let e = error {
        print("getShareMemberDetail failure: \(e)")
    }
})
}

Query sharing details of a user that receives a sharing invitation

API description

- (void)getReceiveMemberDetailWithMemberId:(NSInteger)memberId
                                success:(void(^)(TuyaSmartReceiveMemberDetailModel *model))success
                                failure:(TYFailureError)failure;

Parameters

Parameter Description
memberId The ID of the user that receives the sharing invitation
success The success callback.
failure The failure callback.

Example

ObjC:

- (void)getReceiveMemberDetail {
//self.deviceShare  = [[TuyaSmartHomeDeviceShare alloc] init];

[self.deviceShare getReceiveMemberDetailWithMemberId:memberId success:^(TuyaSmartReceiveMemberDetailModel *model) {

    NSLog(@"getReceiveMemberDetail success");

} failure:^(NSError *error) {

    NSLog(@"getReceiveMemberDetail failure: %@", error);

}];

}

Swift:

func getReceiveMemberDetail() {
deviceShare?.getReceiveMemberDetail(withMemberId: memberId, success: { (model) in
    print("getReceiveMemberDetail success")
}, failure: { (error) in
    if let e = error {
        print("getReceiveMemberDetail failure: \(e)")
    }
})
}

Query a list of users that share a device

API description

- (void)getDeviceShareMemberListWithDevId:(NSString *)devId
                                success:(void(^)(NSArray<TuyaSmartShareMemberModel *> *list))success
                                failure:(TYFailureError)failure;

Parameters

Parameter Description
devId The ID of the device for sharing.
success The success callback.
failure The failure callback.

Example

ObjC:

- (void)getDeviceShareMemberList {
    //self.deviceShare  = [[TuyaSmartHomeDeviceShare alloc] init];

[self.deviceShare getDeviceShareMemberListWithDevId:@"dev_id" success:^(NSArray<TuyaSmartShareMemberModel *> *list) {

    NSLog(@"getDeviceShareMemberList success");

} failure:^(NSError *error) {

    NSLog(@"getDeviceShareMemberList failure: %@", error);

}];

}

Swift:

func getDeviceShareMemberList() {
deviceShare?.getMemberList(withDevId: "dev_id", success: { (model) in
    print("getDeviceShareMemberList success")
}, failure: { (error) in
    if let e = error {
        print("getDeviceShareMemberList failure: \(e)")
    }
})
}

Query the source of a shared device

API description

- (void)getShareInfoWithDevId:(NSString *)devId
                    success:(void(^)(TuyaSmartReceivedShareUserModel *model))success
                    failure:(TYFailureError)failure;

Parameters

Parameter Description
devId The ID of the device for sharing.
success The success callback.
failure The failure callback.

Example

ObjC:

- (void)getShareInfo {
    //self.deviceShare  = [[TuyaSmartHomeDeviceShare alloc] init];

    [self.deviceShare getShareInfoWithDevId:@"dev_id" success:^(TuyaSmartReceivedShareUserModel *model) {

    NSLog(@"get shareInfo success");

} failure:^(NSError *error) {

    NSLog(@"get shareInfo failure: %@", error);

}];

}

Swift:

func getShareInfo() {
deviceShare?.getInfoWithDevId("dev_id", success: { (model) in
    print("getShareInfo success")
}, failure: { (error) in
    if let e = error {
        print("getShareInfo failure: \(e)")
    }
})
}

Remove sharing invitations

Remove a user that initiates sharing invitations

Removes a user that initiates sharing invitations to delete all sharing relationships from the user based on memberId.

API description

- (void)removeShareMemberWithMemberId:(NSInteger)memberId
                            success:(TYSuccessHandler)success
                            failure:(TYFailureError)failure;

Parameters

Parameter Description
memberId The ID of the user that initiates the sharing invitation.
success The success callback.
failure The failure callback.

Example

ObjC:

- (void)removeShareMember {
    //self.deviceShare  = [[TuyaSmartHomeDeviceShare alloc] init];

[self.deviceShare removeShareMemberWithMemberId:memberId success:^{

    NSLog(@"removeShareMember success");

} failure:^(NSError *error) {

    NSLog(@"removeShareMember failure: %@", error);

}];

}

Swift:

func removeShareMember() {
deviceShare?.removeMember(withMemberId: memberId, success: {
    print("removeShareMember success")
}, failure: { (error) in
    if let e = error {
        print("removeShareMember failure: \(e)")
    }
})
}

Remove a user that receives sharing invitations

Removes a user that receives sharing invitations to delete all shared device information from the user based on memberId.

API description

- (void)removeReceiveShareMemberWithMemberId:(NSInteger)memberId
                                    success:(TYSuccessHandler)success
                                    failure:(TYFailureError)failure;

Parameters

Parameter Description
memberId The ID of the user that receives the sharing invitations.
success The success callback.
failure The failure callback.

Example

ObjC:

- (void)removeReceiveMember {
    //self.deviceShare  = [[TuyaSmartHomeDeviceShare alloc] init];

[self.deviceShare removeReceiveShareMemberWithMemberId:memberId success:^{

    NSLog(@"removeReceiveMember success");

} failure:^(NSError *error) {

    NSLog(@"removeReceiveMember failure: %@", error);

}];

}

Swift:

func removeReceiveMember() {
deviceShare?.removeReceiveMember(withMemberId: memberId, success: {
    print("removeReceiveMember success")
}, failure: { (error) in
    if let e = error {
        print("removeReceiveMember failure: \(e)")
    }
})
}

Remove a shared device

API description

- (void)removeDeviceShareWithMemberId:(NSInteger)memberId
                            devId:(NSString *)devId
                            success:(TYSuccessHandler)success
                            failure:(TYFailureError)failure;

Parameters

Parameter Description
memberId The ID of the user that initiates the sharing invitations.
devId The ID of the device to be shared.
success The success callback.
failure The failure callback.

Example

ObjC:

- (void)removeDeviceShare {
    //self.deviceShare  = [[TuyaSmartHomeDeviceShare alloc] init];

[self.deviceShare removeDeviceShareWithMemberId:memberId devId:@"dev_id" success:^{

    NSLog(@"removeDeviceShare success");

} failure:^(NSError *error) {

    NSLog(@"removeDeviceShare failure: %@", error);

}];

}

Swift:

func removeDeviceShare() {
deviceShare?.remove(withMemberId: memberId, devId: "dev_id", success: {
    print("removeDeviceShare success")
}, failure: { (error) in
    if let e = error {
        print("removeDeviceShare failure: \(e)")
    }
})
}

Remove a received shared device

API description

- (void)removeReceiveDeviceShareWithDevId:(NSString *)devId
                                success:(TYSuccessHandler)success
                                failure:(TYFailureError)failure;

Parameters

Parameter Description
devId The ID of the device for sharing.
success The success callback.
failure The failure callback.

Example

ObjC:

- (void)removeDeviceShare {
    //self.deviceShare  = [[TuyaSmartHomeDeviceShare alloc] init];

[self.deviceShare removeReceiveDeviceShareWithDevId:@"dev_id" success:^{

    NSLog(@"removeDeviceShare success");

} failure:^(NSError *error) {

    NSLog(@"removeDeviceShare failure: %@", error);

}];

}

Swift:

func removeDeviceShare() {
deviceShare?.removeReceive(withDevId: "dev_id", success: {
    print("removeDeviceShare success")
}, failure: { (error) in
    if let e = error {
        print("removeDeviceShare failure: \(e)")
    }
})
}

Change nicknames

Change the nickname of a user that initiates sharing invitations

API description

- (void)renameShareMemberNameWithMemberId:(NSInteger)memberId
                                    name:(NSString *)name
                                success:(TYSuccessHandler)success
                                failure:(TYFailureError)failure;

Parameters

Parameter Description
memberId The ID of the user that initiates the sharing invitations.
name The nickname of the user that initiates the sharing invitations.
success The success callback.
failure The failure callback.

Example

ObjC:

- (void)updateShareMemberName {
    //self.deviceShare  = [[TuyaSmartHomeDeviceShare alloc] init];

[self.deviceShare renameShareMemberNameWithMemberId:memberId name:@"new_name" success:^{

    NSLog(@"updateShareMemberName success");

} failure:^(NSError *error) {

    NSLog(@"updateShareMemberName failure: %@", error);

}];

Swift:

func updateShareMemberName() {
deviceShare?.renameShareMemberName(withMemberId: "memberId", name: "new_name", success: {
    print("updateShareMemberName success")
}, failure: { (error) in
    if let e = error {
        print("updateShareMemberName failure: \(e)")
    }
})
}

Change the nickname of a user that receives sharing invitations.

API description

- (void)renameReceiveShareMemberNameWithMemberId:(NSInteger)memberId
                                        name:(NSString *)name
                                        success:(TYSuccessHandler)success
                                        failure:(TYFailureError)failure;

Parameters

Parameter Description
memberId The ID of the user that receives sharing invitations.
name The new nickname.
success The success callback.
failure The failure callback.

Example

ObjC:

- (void)updateReceiveMemberName {
    //self.deviceShare  = [[TuyaSmartHomeDeviceShare alloc] init];

[self.deviceShare renameReceiveShareMemberNameWithMemberId:memberId name:@"new_name" success:^{

    NSLog(@"updateReceiveMemberName success");

} failure:^(NSError *error) {

    NSLog(@"updateReceiveMemberName failure: %@", error);

}];
}

Swift:

func updateReceiveMemberName() {
deviceShare?.renameReceiveMemberName(withMemberId: memberId, name: "new_name", success: {
    print("updateReceiveMemberName success")
}, failure: { (error) in
    if let e = error {
        print("updateReceiveMemberName failure: \(e)")
    }
})
}

Initiate device sharing invitations

Initiate device sharing with another user

API description

- (void)inviteShareWithCountryCode:(NSString *)countryCode
                    userAccount:(NSString *)userAccount
                            devId:(NSString *)devId
                        success:(TYSuccessInt)success
                        failure:(TYFailureError)failure;

Parameters

Parameter Description
countryCode The country code.
userAccount The target user account.
devId The device ID.
success The success callback.
failure The failure callback.

Example

ObjC:

//self.deviceShare  = [[TuyaSmartHomeDeviceShare alloc] init];

[self.deviceShare inviteShareWithCountryCode: Country code userAccount: Target user account devId: List of shared device IDs success:^(int result) {
    //do something
} failure:^(NSError *error) {
    //do something
}];

Swift:

deviceShare.invite(withCountryCode: Country code, userAccount: Target user account, devId: List of shared device IDs) { (_) in
    //do something
} failure: { (_) in
    //do something
}

Confirm a sharing invitation

API description

- (void)confirmInviteShareWithShareId:(NSInteger)shareId
                            success:(TYSuccessHandler)success
                            failure:(TYFailureError)failure;

Parameters

Parameter Description
shareId The ID of the sharing invitation.
success The success callback.
failure The failure callback.

Example

ObjC:

//self.deviceShare = [[TuyaSmartHomeDeviceShare alloc] init];
[self.deviceShare confirmInviteShareWithShareId: Sharing invitation ID success:^{
    //do something
} failure:^(NSError *error) {
    //do something
}];

Swift:

deviceShare.confirmInviteShare(withShareId: Sharing invitation ID) {
    //do something
} failure: { (_) in
    //do something
}

Share devices with a group

Query a list of users that share devices by group ID

The group ID appears on the device panel.

API description

- (void)getGroupShareMemberListWithGroupId:(NSString *)groupId
                                success:(void(^)(NSArray<TuyaSmartShareMemberModel *> *list))success
                                failure:(TYFailureError)failure;

Parameters

Parameter Description
groupId The group ID.
success The success callback.
failure The failure callback.

Example

ObjC:

//self.deviceShare = [[TuyaSmartHomeDeviceShare alloc] init];

[self.deviceShare getGroupShareMemberListWithGroupId: Group ID success:^(NSArray<TuyaSmartShareMemberModel *> * _Nonnull list) {
    //do something
} failure:^(NSError *error) {
    //do something
}];

Swift:

deviceShare.getGroupShareMemberList(withGroupId: Group ID) { (_) in
    //do something
} failure: { (_) in
    //do something
}

Add a user to a sharing group

API description

- (void)addGroupShareToMemberWithSpaceId:(long long)spaceId
                            countyCode:(NSString *)countryCode
                        userAccount:(NSString *)userAccount
                            groupId:(NSString *)groupId
                            success:(TYSuccessID)success
                            failure:(TYFailureError)failure;

Parameters

Parameter Description
spaceId The ID of a specified space.
countryCode The country code.
userAccount The target user account.
groupId The group ID.
success The success callback.
failure The failure callback.

Example

ObjC:

//self.deviceShare = [[TuyaSmartHomeDeviceShare alloc] init];

[self.deviceShare addGroupShareToMemberWithSpaceId: Space ID countyCode: Country code userAccount: Target user account groupId: Group ID success:^(id result) {
    //do something
} failure:^(NSError *error) {
    //do something
}];

Swift:

deviceShare.addGroupShareToMember(withSpaceId: Space ID, countyCode: Country code, userAccount: Target user account, groupId: Group ID) { (_) in
    //do something
} failure: { (_) in
    //do something
}

Remove a user from a sharing group

API description

- (void)removeGroupShareWithRelationId:(NSInteger)relationId
                            groupId:(NSString *)groupId
                            success:(TYSuccessHandler)success
                            failure:(TYFailureError)failure;

Parameters

Parameter Description
relationId The member ID.
groupId The group ID.
success The success callback.
failure The failure callback.

Example

ObjC:

//self.deviceShare = [[TuyaSmartHomeDeviceShare alloc] init];

[self.deviceShare removeGroupShareWithRelationId: User ID groupId: Group ID success:^{
    //do something
} failure:^(NSError *error) {
    //do something
}];

Swift:

deviceShare.removeGroupShare(withRelationId: User ID, groupId: Group ID) {
    //do something
} failure: { (_) in
    //do something
}