设备分享

更新时间:2024-11-12 08:14:28下载pdf

用途

设备分享是指设备的管理者将设备分享给其他人,其他人接受分享后即可使用设备。

主要包括以下功能:

  • 为设备或者群组添加分享,查询设备或者群组当前的被分享用户,删除被分享用户等。
  • 接受设备分享,查询设备或者群组分享者,删除分享者等。

接入

source 'https://github.com/tuya/tuya-pod-specs.git'
platform :ios, '11.0'

target 'Your_Project_Name' do
    pod "ThingSmartBusinessExtensionKit"
end

模型

  • 分享结果

    open class ThingDeviceShareResult : NSObject {
    
        /// The relationship ID.
        open var memberId: Int
    
        /// The nickname.
        open var nickName: String
    
        /// The username that is the phone number or email address.
        open var userName: String
    }
    
  • 分享信息

    open class ThingDeviceShareInfo : NSObject {
    
        open var content: String
    
        open var code: String
    
        open var shortUrl: String
    }
    
  • 分享方式

    public enum ThingDeviceShareType : Int {
        case account = 0
        case QQ = 1
        case wechat = 2
        case message = 3
        case email = 4
        case copy = 5
        case more = 6
        case contact = 7
    }
    
  • 分享码信息

    open class ThingDeviceShareCodeInfo : NSObject {
    
        open var appId: String
    
        /// Device/Group ID
        open var resId: String
    
        /// Resource type: 1 Device type, 2 Group type
        open var resType: Int32
    
        /// Device/Group icon
        open var resIcon: String
    
        /// Device/Group name
        open var resName: String
    
        /// Name of the sharer
        open var nickName: String
    
        /// Sharing channels
        open var shareSource: ThingDeviceShareType
    
        /// Group ID of the sharer
        open var groupId: Int64
    }
    
  • 分享者信息

    open class ThingDeviceSharer : NSObject {
    
        /// The relationship ID.
        open var memberId: Int
    
        /// The nickname.
        open var nickName: String
    
        /// The username that is the phone number or email address.
        open var userName: String
    }
    
    
  • 分享者详细信息

    open class ThingDeviceSharerDetail : NSObject {
    
        /// The shared devices
        open var devices: [ThingSmartShareDeviceModel]
    
        /// The account information, email, or phone number.
        open var mobile: String
    
        /// The user nickname.
        open var name: String
    
        /// The remarks.
        open var remarkName: String
    }
    
  • 分享管理类

    open class ThingDeviceShareManager : NSObject {
        //...
    }
    

分享者相关接口

判断设备或者群组是否支持分享

入参 类型 说明
resId String 被分享设备或群组的 ID
resType ThingShareResType 资源类型,设备为 ThingShareResTypeDevice,群组为 ThingShareResTypeGroup
success (Bool) -> Void 成功回调
failure (Error) -> Void 失败回调
    /// Determine whether the device or group supports sharing
    /// - Parameters:
    ///   - resId: The ID of the device or group to be shared
    ///   - resType: ThingShareResTypeDevice if a device is shared, otherwise ThingShareResTypeGroup
    ///   - success: Success callback, true if sharing is supported, otherwise false.
    ///   - failure: Failure callback
    open class func supportShare(_ resId: String, resType:ThingShareResType, success: @escaping (Bool) -> Void, failure: @escaping (Error) -> Void)

获取设备或者群组的剩余分享次数

入参 类型 说明
resId String 被分享设备或群组的 ID
resType ThingShareResType 资源类型,设备为 ThingShareResTypeDevice,群组为 ThingShareResTypeGroup
success (Int32) -> Void 成功回调
failure (Error) -> Void 失败回调
    /// Get the remaining share times of the device or group.
    /// For now, group does not support this feature. In short, it can only be used to check the remaining share times of the device.
    /// - Parameters:
    ///   - resId: The ID of the device or group to be shared
    ///   - resType: ThingShareResTypeDevice if a device is shared, otherwise ThingShareResTypeGroup
    ///   - success: Success callback. Unlimited if times is -1
    ///   - failure: Failure callback
    open class func remainingShareTimes(_ resId: String, resType:ThingShareResType, success: @escaping (Int32) -> Void, failure: @escaping (Error) -> Void)

将设备或者群组分享给用户

将群组共享给用户,并不会更新到最近分享的联系人列表中。

入参 类型 说明
resId String 被分享设备或群组的 ID
resType ThingShareResType 资源类型,设备为 ThingShareResTypeDevice,群组为 ThingShareResTypeGroup
spaceId Int64 设备或群组的 homeid
userAccount String 用户的账号
success (ThingDeviceShareResult) -> Void 成功回调
failure (Error) -> Void 失败回调
    /// Share a device or group to another user
    /// - Parameters:
    ///   - resId: The ID of the device or group to be shared
    ///   - resType: ThingShareResTypeDevice if a device is shared, otherwise ThingShareResTypeGroup
    ///   - spaceId: The home ID of the device or group
    ///   - userAccount: the account of the user shared to
    ///   - success: Success callback with the sharing result
    ///   - failure: Failure callback
    open class func share(_ resId: String, resType: ThingShareResType, spaceId: Int64, userAccount: String, success: @escaping (ThingDeviceShareResult) -> Void, failure: @escaping (Error) -> Void)

移除被分享者

入参 类型 说明
memberId Int 被分享者的 memberId
resId String 被分享设备或群组的 ID
resType ThingShareResType 资源类型,设备为 ThingShareResTypeDevice,群组为 ThingShareResTypeGroup
success () -> Void 成功回调
failure (Error) -> Void 失败回调
    /// Remove the receiver of device or group
    /// - Parameters:
    ///   - memberId: The ID of the member to be removed
    ///   - resId: The ID of device or group to be shared
    ///   - resType: ThingShareResTypeDevice if a device is shared, otherwise ThingShareResTypeGroup
    ///   - success: Success callback
    ///   - failure: Failure callback
    open class func removeReceiver(_ memberId: Int, resId: String, resType: ThingShareResType, success: @escaping () -> Void, failure: @escaping (Error) -> Void)

获取设备或群组的被分享者列表

入参 类型 说明
resId String 被分享设备或群组的 ID
resType ThingShareResType 资源类型,设备为 ThingShareResTypeDevice,群组为 ThingShareResTypeGroup
page Int32 页码,从 1 开始。对群组无效,群组会一次性返回所有数据
pageSize Int32 分页大小。对群组无效,群组会一次性返回所有数据
success ([ThingSmartShareMemberModel]) -> Void 成功回调
failure (Error) -> Void 失败回调
    /// Get receivers of a device or group shared to
    /// - Parameters:
    ///   - resId: The ID of device or group to be shared
    ///   - resType: ThingShareResTypeDevice if a device is shared, otherwise ThingShareResTypeGroup
    ///   - page: The number of page to fetch, starting from 1. This parameter is useful only for devices. For groups, it will return all members at once.
    ///   - pageSize: The size of page to fetch, which must be greater than zero. This parameter is useful only for device. For groups, it will return all members at once.
    ///   - success: Success callback with a list of ThingSmartShareMemberModel
    ///   - failure: Failure callback
    open class func receivers(_ resId: String, resType: ThingShareResType, page: Int32, pageSize: Int32, success: @escaping ([ThingSmartShareMemberModel]?) -> Void, failure: @escaping (Error) -> Void)

更新分享有效期

群组不能更新分享的有效期。

入参 类型 说明
memberId Int 被分享者的 memberId
resId String 被分享设备或群组的 ID
resType ThingShareResType 资源类型,设备为 ThingShareResTypeDevice,群组为 ThingShareResTypeGroup
mode ThingShareValidationType 模式
endTime Int64 结束时间,单位毫秒,只有 ThingShareValidationTypePeriod 才有用
success () -> Void 成功回调
failure (Error) -> Void 失败回调
    /// Update the sharing expiration date
    /// - Parameters:
    ///   - memberId: The ID of a member to be updated
    ///   - resId: The ID of the device or group to be shared
    ///   - resType: ThingShareResTypeDevice if a device is shared, otherwise ThingShareResTypeGroup
    ///   - mode: ThingShareValidationTypeForever if the sharing is valid forever, otherwise ThingShareValidationTypePeriod
    ///   - endTime: Timestamp in milliseconds if mode is ThingShareValidationTypePeriod, or it will be ignored.
    ///   - success: Success callback
    ///   - failure: Failure callback
    open class func updateShareExpirationDate(_ memberId: Int, resId: String, resType: ThingShareResType, mode: ThingShareValidationType, endTime: Int64, success: @escaping () -> Void, failure: @escaping (Error) -> Void)

获取最近分享的联系人列表

入参 类型 说明
success ([ThingSmartShareMemberModel]) -> Void 成功回调
failure (Error) -> Void 失败回调
    /// Get members who were recently shared
    /// - Parameters:
    ///   - success: Success callback with a list of ThingSmartShareMemberModel
    ///   - failure: Failure callback
    open class func relationMembers(_ success: @escaping ([ThingSmartShareMemberModel]?) -> Void, failure: @escaping (Error) -> Void)

移除最近分享的联系人

入参 类型 说明
uid String 用户 ID
success () -> Void 成功回调
failure (Error) -> Void 失败回调
    /// Remove members who were recently shared
    /// - Parameters:
    ///   - uid: The ID of a member to be removed
    ///   - success: Success callback
    ///   - failure: Failure callback
    open class func removeRelationMember(_ uid: String, success: @escaping () -> Void, failure: @escaping (Error) -> Void)

生成分享信息

生成分享信息,用于三方分享等。群组无法使用此种分享方式。

入参 类型 说明
resId String 被分享设备或群组的 ID
resType ThingShareResType 资源类型,设备为 ThingShareResTypeDevice,群组为 ThingShareResTypeGroup
spaceId Int64 设备所在的家庭 ID
shareType ThingDeviceShareType 分享渠道类型
shareCount Int32 分享次数
success (ThingDeviceShareInfo) -> Void 成功回调
failure (Error) -> Void 失败回调
    /// Create a share code to share a device
    /// For now, group does not support this feature. In short, it can only be used to share a device.
    /// - Parameters:
    ///   - resId: The ID of the device or group to be shared
    ///   - resType: ThingShareResTypeDevice if a device is shared, otherwise ThingShareResTypeGroup
    ///   - spaceId: The home ID of the device or group
    ///   - shareType: The type to share the code, ie: email, message, etc. For more information, see ThingDeviceShareType
    ///   - shareCount: The count of valid uses for the share code
    ///   - success: Success callback
    ///   - failure: Failure callback
    open class func createShareInfo(_ resId: String, resType:ThingShareResType, spaceId: Int64, shareType: ThingDeviceShareType, shareCount: Int32, success: @escaping (ThingDeviceShareInfo) -> Void, failure: @escaping (Error) -> Void)

如果您使用分享码进行分享,请为您的 App 配置通用链接和 scheme。

设备分享 设备分享

其中,渠道码可以在涂鸦开发者平台上查看。

设备分享

并且在您的 AppDelegate 中,实现 optional func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool 方法,将分享邀请展示给用户。

    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
        //weburl  https://appScheme.applink.smart321.com/share?link=appScheme://share_link?code=MN54PUQQ
        guard let webUrl = userActivity.webpageURL else { return false }
       let params = ["activityType": NSUserActivityTypeBrowsingWeb, "webUrl": webUrl.absoluteString]
        let components = NSURLComponents(string: webUrl.absoluteString)
        guard let items = components?.queryItems else {return false}
        let item = items.first {$0.name == "link"}

        guard let link = item?.value?.removingPercentEncoding else {return false}
        let linkComponents = URLComponents(string: link)
        guard let linkItems = linkComponents?.queryItems, let host = linkComponents?.host else {return false}

        if (host == "share_link") {
            let codeItem = linkItems.first {$0.name == "code"}
            if codeItem == nil {return false}

            //show share invate to user
            ...
            return true
        }

        // other logic...
    }

被分享者相关接口

验证分享码

入参 类型 说明
code String 分享码
success (Bool) -> Void 成功回调
failure (Error) -> Void 失败回调
    /// Validate a share code
    /// - Parameters:
    ///   - code: The share code
    ///   - success: Success callback, true if valid
    ///   - failure: Failure callback
    open class func validate(_ code: String, success: @escaping (Bool) -> Void, failure: @escaping (Error) -> Void)

获取分享码信息

入参 类型 说明
code String 分享码
success (ThingDeviceShareCodeInfo) -> Void 成功回调
failure (Error) -> Void 失败回调
    /// Get info of the share code
    /// - Parameters:
    ///   - code: The share code
    ///   - success: Success callback, with ThingDeviceShareCodeInfo
    ///   - failure: Failure callback
    open class func shareCodeInfo(_ code: String, success: @escaping (ThingDeviceShareCodeInfo) -> Void, failure: @escaping (Error) -> Void)

接受分享邀请

入参 类型 说明
code String 分享码
success () -> Void 成功回调
failure (Error) -> Void 失败回调
    /// Accept the share invitation
    /// - Parameters:
    ///   - code: The share code
    ///   - success: Success callback
    ///   - failure: Failure callback
    open class func accept(_ code: String, success: @escaping () -> Void, failure: @escaping (Error) -> Void)

移除分享者分享的设备或者群组

入参 类型 说明
resId String 被分享设备或群组的 ID
resType ThingShareResType 资源类型,设备为 ThingShareResTypeDevice,群组为 ThingShareResTypeGroup
success () -> Void 成功回调
failure (Error) -> Void 失败回调
    /// Remove a share of device or group
    /// - Parameters:
    ///   - resId: The ID of the device or group to be shared
    ///   - resType: ThingShareResTypeDevice if a device is shared, otherwise ThingShareResTypeGroup
    ///   - success: Success callback
    ///   - failure: Failure callback
    open class func removeShare(_ resId: String, resType: ThingShareResType, success: @escaping () -> Void, failure: @escaping (Error) -> Void)

获取分享者列表

入参 类型 说明
success () -> Void 成功回调
failure (Error) -> Void 失败回调
    /// Get the sharers who share the device or group
    /// - Parameters:
    ///   - success: Success callback, with a list of ThingDeviceSharer
    ///   - failure: Failure callback
    open class func sharers(_ success: @escaping ([ThingDeviceSharer]?) -> Void, failure: @escaping (Error) -> Void)

获取设备或者群组的分享者名称

入参 类型 说明
resId String 被分享设备或群组的 ID
resType ThingShareResType 资源类型,设备为 ThingShareResTypeDevice,群组为 ThingShareResTypeGroup
success (String) -> Void 成功回调
failure (Error) -> Void 失败回调
    /// Get the name of the sharer
    /// - Parameters:
    ///   - resId: The ID of the device or group to be shared
    ///   - resType: ThingShareResTypeDevice if a device is shared, otherwise ThingShareResTypeGroup
    ///   - success: Success callback
    ///   - failure: Failure callback
    open class func sharerName(_ resId: String, resType: ThingShareResType, success: @escaping (String) -> Void, failure: @escaping (Error) -> Void)

获取分享者详情

入参 类型 说明
memberId Int 分享者的 memberId
success (ThingDeviceSharerDetail) -> Void 成功回调
failure (Error) -> Void 失败回调
    /// Get the detail of sharer who shares the device or group
    /// - Parameters:
    ///   - memberId: The ID of sharer who shares the device or group
    ///   - success: Success callback, with ThingDeviceSharerDetail
    ///   - failure: Failure callback
    open class func sharerDetail(_ memberId: Int, success: @escaping (ThingDeviceSharerDetail) -> Void, failure: @escaping (Error) -> Void)

移除分享者

入参 类型 说明
memberId Int 分享者的 memberId
success () -> Void 成功回调
failure (Error) -> Void 失败回调
    /// Remove the sharer
    /// - Parameters:
    ///   - memberId: The ID of the sharer to be removed
    ///   - success: Success callback
    ///   - failure: Failure callback
    open class func removeSharer(_ memberId: Int, success: @escaping () -> Void, failure: @escaping (Error) -> Void)

修改分享者名称备注

入参 类型 说明
memberId Int 分享者的 memberId
name String 名称备注
success () -> Void 成功回调
failure (Error) -> Void 失败回调
    /// Update the name of the sharer
    /// - Parameters:
    ///   - memberId: The ID of sharer to be updated
    ///   - name: The new name of the sharer
    ///   - success: Success callback
    ///   - failure: Failure callback
    open class func updateSharer(_ memberId: Int, name: String, success: @escaping () -> Void, failure: @escaping (Error) -> Void)

更多信息,参考 Demo