腾讯云 IM iOS 自定义消息,用TXIMSDK_TUIKit_iOS_Professional

212 阅读1分钟

image.png

由于要用老项目里的TXIMSDK_TUIKit_iOS_Professional来进行自定义消息,官网的文档都是最新的,很多方法已经缺失,我照着文章 juejin.cn/post/707853… ,居然连TUIMessageDataProvider这个类都没有,当然最后没有成功。在网上找了一个老版本的七百多页的文档,也是有些方法属性没有。无奈只能看下之前项目是怎么实现的了。

1.子类化TUIMessageCell

注意fillWithData方法

2.子类化TUIMessageCellData

注意这里没有啥重写父类的 getCellData: 方法

自己定义个方法如-(void)setCellDataWithMsg:(V2TIMMessage *)msg;

3.自定义MyChatViewController继承自TUIChatController

self.delegate = self

实现代理

extension MyChatViewController: TUIChatControllerDelegate {

    func chatController(_ controller: TUIChatController!, didSendMessage msgCellData: TUIMessageCellData!) {

        

    }

    

    func chatController(_ controller: TUIChatController!, onShowMessageData cellData: TUIMessageCellData!) -> TUIMessageCell! {

        

        let msg = cellData.innerMessage

        let isCustomMsg = msg.elemType == .ELEM_TYPE_CUSTOM

        if isCustomMsg, let dic = try? JSONSerialization.jsonObject(with: msg.customElem.data, options: .allowFragments) as? Dictionary<AnyHashable, Any> {

            if let businessID = dic["businessID"] as? String, businessID == "friend_gift" {

  


                if let friendCellData = cellData as? SL_CustomFriendGiftCellData {

                    

                    let friendCell = SL_CustomFriendGiftCell(style: .default, reuseIdentifier: "friendCell")

                    friendCellData.setCellDataWithMsg(msg)

                    friendCell.fill(with: friendCellData)

                    return friendCell

                }

                

            }

        }

        

        return nil;

    }

    

    func chatController(_ chatController: TUIChatController!, onSelect cell: TUIInputMoreCell!) {

        let chatModel = SL_ChatMinutes_Model()

        chatModel.audio = 100.0

        chatModel.video = 100.0

        var callType: CallType = .audio

        if let title = cell?.data?.title, title == "语音" {

            callType = .audio

        } else if let title = cell?.data?.title, title == "视频" {

            // 发送消息

            sendFriend_giftMsg()

            

        }

        

    }

    func sendFriend_giftMsg() {

        let dict:[String:String] = ["giftId""giftId",

                                    "content""content",

                                    "businessID": "friend_gift"]

        

        let data = try? JSONSerialization.data(withJSONObject:dict , options: .prettyPrinted)

        let msg = V2TIMManager.sharedInstance()?.createCustomMessage(data)

        let cellData = SL_CustomFriendGiftCellData(direction: .MsgDirectionOutgoing)

        cellData.innerMessage = msg!

        sendMessage(cellData)

    }

    func chatController(_ controller: TUIChatController!, onSelectMessageAvatar cell: TUIMessageCell!) {

        // 跳转用户资料

    }

    

    func chatController(_ controller: TUIChatController!, onSelectMessageContent cell: TUIMessageCell!) {

        

    }

    

    func chatController(_ controller: TUIChatController!, onNewMessage msg: V2TIMMessage!) -> TUIMessageCellData? {

        

        if msg.elemType == .ELEM_TYPE_CUSTOM,

            let dic = try? JSONSerialization.jsonObject(with: msg.customElem.data, options: .allowFragments) as? Dictionary<AnyHashable, Any> {

            if let businessID = dic["businessID"] as? String, businessID == "friend_gift" {

                let direction: TMsgDirection = msg.isSelf ? .MsgDirectionOutgoing : .MsgDirectionIncoming

                let friendCellData = SL_CustomFriendGiftCellData(direction: direction)

                friendCellData.setCellDataWithMsg(msg)

                return friendCellData

            }

            

        }

        return nil

    }

}

可以正常发送显示自定义消息了