共享设备

更新时间:2023-05-25 06:23:48

当用户需要将家庭下某一个设备单独提供给其他用户操作时,可将设备共享给其他用户。收到共享设备的用户,可在自身账号下对设备进行简单操作。

每个设备最多可以分享给 20 个其他用户。

接入

类名 说明
TuyaSmartHomeDeviceShare 提供设备共享相关的功能

Objc:

#import <TuyaSmartDeviceKit/TuyaSmartHomeDeviceShare.h>

Swift:

import TuyaSmartDeviceKit

添加共享

共享一个或多个设备

共享一个或多个设备给指定用户。另外,每个设备最多可以分享给 20 个其他用户。

接口 addShareWithHomeId:countryCode:userAccount:devIds:failure:addDeviceShareWithHomeId:countryCode:userAccount:devId:success:failure: 即将废弃,请及时升级。

接口说明

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

参数说明

参数 说明
requestModel 请求对象
success 成功回调
failure 失败回调

TuyaSmartDeviceShareRequestModel 说明

@interface TuyaSmartDeviceShareRequestModel : NSObject

/// 设备所属的家庭 ID
@property (nonatomic, assign) long long homeID;

/// 共享对象的国家码
@property (nonatomic, copy) NSString *countryCode;

/// 共享对象的账号
@property (nonatomic, copy) NSString *userAccount;

/// 共享的设备 ID 列表
@property (nonatomic, copy) NSArray<NSString *> *devIds;

@end

示例代码

Objc:

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

TuyaSmartDeviceShareRequestModel *model = [TuyaSmartDeviceShareRequestModel new];
model.homeID = 家庭 ID;
model.countryCode = 国家码;
model.userAccount = 共享对象的账号;
model.devIds = 共享的设备 ID 列表;

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

Swift:

func addMemberShare() {
var model = TuyaSmartDeviceShareRequestModel.init()
model.homeID = 家庭 ID
model.countryCode =
model.userAccount = 共享对象的账号
model.devIds = 共享的设备 ID 列表

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

共享多个设备

共享多个设备给指定用户,会将要共享的设备追加到指定用户的所有共享中。另外,每个设备最多可以分享给 20 个其他用户。

接口说明

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

参数说明

参数 说明
memberId 共享对象的 ID
success 成功回调
failure 失败回调

示例代码

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

[旧版] 添加多个设备共享并覆盖

共享多个设备给指定用户,会将指定用户的以前所有共享覆盖掉。另外,每个设备最多可以分享给 20 个其他用户。

该接口即将废弃,请使用addDeviceShareWithRequestModel:success:failure:

接口说明

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

参数说明

参数 说明
homeId 设备所属的家庭 ID
countryCode 共享对象的国家码
userAccount 共享对象的账号
devIds 共享的设备 ID 列表
success 成功回调
failure 失败回调

示例代码

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

[旧版] 设备添加共享

共享单个设备给指定用户,会将要共享的设备追加到指定用户的所有共享中。

该接口即将废弃,请使用 addDeviceShareWithRequestModel:success:failure:

接口说明

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

参数说明

参数 说明
homeId 设备的家庭 ID
countryCode 共享对象的国家码
userAccount 共享对象的账号
devId 共享设备的 ID
success 成功回调
failure 失败回调

示例代码

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

查询共享关系

查询所有收到共享的用户

接口说明

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

参数说明

参数 说明
success 成功回调
failure 失败回调

示例代码

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

查询家庭下所有主动共享的用户

接口 getShareMemberListWithHomeId:success:failure: 即将废弃,请及时升级。

接口说明

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

参数说明

参数 说明
spaceId 空间 ID
success 成功回调
failure 失败回调

示例代码

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

[旧版] 查询家庭下所有主动共享的用户

该接口即将废弃,请使用 getShareMemberListWithSpaceId:success:failure:

接口说明

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

参数说明

参数 说明
homeId
success 成功回调
failure 失败回调

示例代码

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

查询单个主动共享的用户共享数据

接口说明

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

参数说明

参数 说明
memberId 共享用户的 ID
success 成功回调
failure 失败回调

示例代码

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

查询单个收到共享的用户共享数据

接口说明

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

参数说明

参数 说明
memberId 收到设备共享者的用户 ID
success 成功回调
failure 失败回调

示例代码

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

查询单设备共享用户

接口说明

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

参数说明

参数 说明
devId 共享的设备 ID
success 成功回调
failure 失败回调

示例代码

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

查询设备的共享来源

接口说明

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

参数说明

参数 说明
devId 共享的设备 ID
success 成功回调
failure 失败回调

示例代码

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

移除共享

删除主动共享者

共享者通过 memberId 删除与这个关系用户的所有共享关系(用户维度删除)。

接口说明

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

参数说明

参数 说明
memberId 共享用户 ID
success 成功回调
failure 失败回调

示例代码

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

删除收到共享者

被共享者通过 memberId 删除收到这个关系用户的所有共享设备信息。

接口说明

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

参数说明

参数 说明
memberId 收到共享的用户 ID
success 成功回调
failure 失败回调

示例代码

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

单设备删除共享

接口说明

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

参数说明

参数 说明
memberId 共享用户的 ID
devId 共享设备的 ID
success 成功回调
failure 失败回调

示例代码

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

删除收到的共享设备

接口说明

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

参数说明

参数 说明
devId 共享的设备 ID
success 成功回调
failure 失败回调

示例代码

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

修改备注名

修改主动共享者的昵称

接口说明

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

参数说明

参数 说明
memberId 共享用户的 ID
name 共享用户的新昵称
success 成功回调
failure 失败回调

示例代码

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

修改收到共享者的昵称

接口说明

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

参数说明

参数 说明
memberId 将设备共享给您的用户 ID
name 新的昵称
success 成功回调
failure 失败回调

示例代码

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

共享邀请

向其他用户发送共享设备的邀请

接口说明

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

参数说明

参数 说明
countryCode 国家码
userAccount 用户账号
devId 设备 ID
success 成功回调
failure 失败回调

示例代码

Objc:

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

[self.deviceShare inviteShareWithCountryCode:国家码 userAccount:共享对象的账号 devId:共享的设备 ID 列表 success:^(int result) {
    //do something
} failure:^(NSError *error) {
    //do something
}];

Swift:

deviceShare.invite(withCountryCode: 国家码, userAccount: 共享对象的账号, devId: 共享的设备 ID 列表) { (_) in
    //do something
} failure: { (_) in
    //do something
}

确认共享邀请

接口说明

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

参数说明

参数 说明
shareId 共享 ID
success 成功回调
failure 失败回调

示例代码

Objc:

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

Swift:

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

群组共享

按群组 ID 返回设备共享用户

群组 ID 会显示在面板上。

接口说明

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

参数说明

参数 说明
groupId 群组 ID
success 成功回调
failure 失败回调

示例代码

Objc:

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

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

Swift:

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

共享群组

接口说明

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

参数说明

参数 说明
spaceId 空间 ID
countryCode 国家码
userAccount 用户账号
groupId 群组 ID
success 成功回调
failure 失败回调

示例代码

Objc:

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

[self.deviceShare addGroupShareToMemberWithSpaceId:空间 ID countyCode:国家码 userAccount:共享对象的账号 groupId:分组 ID success:^(id result) {
    //do something
} failure:^(NSError *error) {
    //do something
}];

Swift:

deviceShare.addGroupShareToMember(withSpaceId: 空间 ID, countyCode: 国家码, userAccount: 共享对象的账号, groupId: 分组 ID) { (_) in
    //do something
} failure: { (_) in
    //do something
}

将设备共享组从成员中移除

接口说明

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

参数说明

参数 说明
relationId 成员 ID
groupId 群组 ID
success 成功回调
failure 失败回调

示例代码

Objc:

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

[self.deviceShare removeGroupShareWithRelationId:成员 ID groupId:群组 ID success:^{
    //do something
} failure:^(NSError *error) {
    //do something
}];

Swift:

deviceShare.removeGroupShare(withRelationId: 成员 ID, groupId: 群组 ID) {
    //do something
} failure: { (_) in
    //do something
}