集成融云即時(shí)通訊(Swift)

1、cocoapod集成

pod 'RongCloudIM/IMLib'
pod 'RongCloudIM/IMKit'

2、橋接文件導(dǎo)入

//融云
#import <RongIMKit/RongIMKit.h>

3、設(shè)置appkey、屬性、代理

RCIM.shared().initWithAppKey(RongClondAppkey)
        RCIM.shared().connectionStatusDelegate = self
        RCIM.shared().userInfoDataSource = self
        RCIM.shared().receiveMessageDelegate = self
        RCIM.shared().groupInfoDataSource = self
        
        // 設(shè)置全局的屬性
        RCIM.shared().globalMessageAvatarStyle = RCUserAvatarStyle.USER_AVATAR_CYCLE
        RCIM.shared().globalConversationAvatarStyle = RCUserAvatarStyle.USER_AVATAR_CYCLE
        RCIM.shared().globalMessagePortraitSize = CGSize.init(width: 50, height: 50)
        RCIM.shared().globalConversationPortraitSize = CGSize.init(width: 50, height: 50)
        RCIM.shared().globalNavigationBarTintColor = colorWithRGB(R: 10, G: 96, B: 254)
        UIApplication.shared.applicationIconBadgeNumber = 0

4、代理方法

//其他設(shè)備登錄時(shí)調(diào)用此方法
func onRCIMConnectionStatusChanged(_ status: RCConnectionStatus) {
        print("%ld",status)
        if status == RCConnectionStatus.ConnectionStatus_KICKED_OFFLINE_BY_OTHER_CLIENT{
            var sweet = SweetAlert.init()
            sweet = sweet.showAlert("下線通知", subTitle: "該用戶在其他設(shè)備上登錄,你已被迫下線", style: AlertStyle.none, buttonTitle: "確定", action: { (bool) in
                
            })
            sweet.animateAlert()
        }
    }
    /*!
     *獲取用戶信息
     */
    func getUserInfo(withUserId userId: String!, completion: ((RCUserInfo?) -> Void)!) {
        if userId == nil{
            return
        }
        let userinfo = RCUserInfo.init(userId: "text1", name: "TTT", portrait: "http://localhost/簡(jiǎn)單HTML/citi標(biāo)簽.html")
        completion(userinfo)
    }
    /*!
     *接收消息的回調(diào)方法
     */
    func onRCIMReceive(_ message: RCMessage!, left: Int32) {
        
        print("%@",message)
    }
    
    /**
     *  獲取群組信息
     */
    func getGroupInfo(withGroupId groupId: String!, completion: ((RCGroup?) -> Void)!) {
        if groupId == nil {
            return
        }
    }

5、上面這些集成基本上算完成了,下面是業(yè)務(wù)方法
注冊(cè):對(duì)于某個(gè)App來說都有注冊(cè)賬號(hào),當(dāng)注冊(cè)成功時(shí),可以把注冊(cè)時(shí)的相應(yīng)信息傳導(dǎo)后臺(tái),讓后臺(tái)去對(duì)融云進(jìn)行注冊(cè)。
登錄:這個(gè)需要前端去進(jìn)行操作,下面是登錄方法

RCIM.shared().connect(withToken: token, success: { (userId: String?) in
            let username = "TT"
            let iconurl = ""
            let currentUserInfo = RCUserInfo.init(userId: userId!, name: username, portrait: iconurl)
            RCIMClient.shared().currentUserInfo = currentUserInfo
        }, error: { (status: RCConnectErrorCode) in
            print("登錄錯(cuò)誤碼為:%ld",status)
        }) { 
            //token過期或者不正確。
            //如果設(shè)置了token有效期并且token過期,請(qǐng)重新請(qǐng)求您的服務(wù)器獲取新的token
            //如果沒有設(shè)置token有效期卻提示token錯(cuò)誤,請(qǐng)檢查您客戶端和服務(wù)器的appkey是否匹配,還有檢查您獲取token的流程。
            UserDefaults.setValue("", forKey: "預(yù)留K")
            HUDBonlytext(text: "Token過期或錯(cuò)誤", view: UIApplication.shared.keyWindow!)
        }

退出:這個(gè)很簡(jiǎn)單只有一個(gè)方法

/**
     *  退出融云服務(wù)器
     */
    func logoutRongCloud() {
        RCIM.shared().logout()
    }

6、通訊列表頁
設(shè)置類型聊天類型:私人聊天,聊天室,群組,討論組等,注意設(shè)置類型后面一定要加上rawValue,不然列表頁什么都不顯示

override func viewDidLoad() {
        super.viewDidLoad()
        
        self.setDisplayConversationTypes([RCConversationType.ConversationType_PRIVATE.rawValue, RCConversationType.ConversationType_GROUP.rawValue,RCConversationType.ConversationType_DISCUSSION.rawValue])
        
        view.backgroundColor = colorWithHexString(hex: "0xf6f6f6")
        self.conversationListTableView.separatorStyle = UITableViewCellSeparatorStyle.none
        
    }

點(diǎn)擊cell,進(jìn)入聊天界面

//點(diǎn)擊Cell用戶聊天
    override func onSelectedTableRow(_ conversationModelType: RCConversationModelType, conversationModel model: RCConversationModel!, at indexPath: IndexPath!) {
        
        let chat = ChatViewController()//可以自己創(chuàng)建,也可以寫原生控制器RCConversationViewController
        
        chat.conversationType = model.conversationType
        chat.targetId = model.targetId
        
        self .present(chat, animated: true, completion: nil)
    }

點(diǎn)擊列表界面頭像方法

    override func didTapCellPortrait(_ model: RCConversationModel!) {
        print("%@",model)
    }

cell即將顯示的時(shí)候

    override func willDisplayConversationTableCell(_ cell: RCConversationBaseCell!, at indexPath: IndexPath!) {
        
        //獲取模型
        let conversationModel = (self.conversationListDataSource[indexPath.row]) as! RCConversationModel
        
        if conversationModel.conversationType == RCConversationType.ConversationType_PRIVATE{
            let conversationCell = cell as! RCConversationCell
            conversationCell.conversationTitle.backgroundColor = UIColor.red
        }
    }

7、聊天界面
即將顯示Cell的代理方法

    override func willDisplayMessageCell(_ cell: RCMessageBaseCell!, at indexPath: IndexPath!) {
        print("%@",cell)
        if cell.isKind(of: RCTextMessageCell.self) {
            let textCell = cell as! RCTextMessageCell
            textCell.textLabel.textColor = UIColor.blue
        } else if cell.isKind(of: RCMessageCell.self) {
            let messageCell = cell as! RCMessageCell
            messageCell.nicknameLabel.textColor = UIColor.red
        }
    }

點(diǎn)擊頭像代理方法

    override func didTapCellPortrait(_ userId: String!) {
        print("%@",userId)
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 2017.02.22 可以練習(xí),每當(dāng)這個(gè)時(shí)候,腦袋就犯困,我這腦袋真是神奇呀,一說讓你做事情,你就犯困,你可不要太...
    Carden閱讀 1,490評(píng)論 0 1
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,080評(píng)論 4 61
  • 例如1、:創(chuàng)建一個(gè)類Person再創(chuàng)建一個(gè)字類SonPerson繼承自Person 在SonPerson中輸出: ...
    三歲就很乖閱讀 778評(píng)論 0 0
  • 平靜的水面因?yàn)榍优澈婉娉謴奈催_(dá)到沸點(diǎn)
    我想寫點(diǎn)東西閱讀 150評(píng)論 0 0

友情鏈接更多精彩內(nèi)容