美文网首页
集成融云即时通讯(Swift)

集成融云即时通讯(Swift)

作者: 未来々人生 | 来源:发表于2017-06-15 14:33 被阅读2099次

1、cocoapod集成

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

2、桥接文件导入

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

3、设置appkey、属性、代理

RCIM.shared().initWithAppKey(RongClondAppkey)
        RCIM.shared().connectionStatusDelegate = self
        RCIM.shared().userInfoDataSource = self
        RCIM.shared().receiveMessageDelegate = self
        RCIM.shared().groupInfoDataSource = self
        
        // 设置全局的属性
        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、代理方法

//其他设备登录时调用此方法
func onRCIMConnectionStatusChanged(_ status: RCConnectionStatus) {
        print("%ld",status)
        if status == RCConnectionStatus.ConnectionStatus_KICKED_OFFLINE_BY_OTHER_CLIENT{
            var sweet = SweetAlert.init()
            sweet = sweet.showAlert("下线通知", subTitle: "该用户在其他设备上登录,你已被迫下线", 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/简单HTML/citi标签.html")
        completion(userinfo)
    }
    /*!
     *接收消息的回调方法
     */
    func onRCIMReceive(_ message: RCMessage!, left: Int32) {
        
        print("%@",message)
    }
    
    /**
     *  获取群组信息
     */
    func getGroupInfo(withGroupId groupId: String!, completion: ((RCGroup?) -> Void)!) {
        if groupId == nil {
            return
        }
    }

5、上面这些集成基本上算完成了,下面是业务方法
注册:对于某个App来说都有注册账号,当注册成功时,可以把注册时的相应信息传导后台,让后台去对融云进行注册。
登录:这个需要前端去进行操作,下面是登录方法

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("登录错误码为:%ld",status)
        }) { 
            //token过期或者不正确。
            //如果设置了token有效期并且token过期,请重新请求您的服务器获取新的token
            //如果没有设置token有效期却提示token错误,请检查您客户端和服务器的appkey是否匹配,还有检查您获取token的流程。
            UserDefaults.setValue("", forKey: "预留K")
            HUDBonlytext(text: "Token过期或错误", view: UIApplication.shared.keyWindow!)
        }

退出:这个很简单只有一个方法

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

6、通讯列表页
设置类型聊天类型:私人聊天,聊天室,群组,讨论组等,注意设置类型后面一定要加上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
        
    }

点击cell,进入聊天界面

//点击Cell用户聊天
    override func onSelectedTableRow(_ conversationModelType: RCConversationModelType, conversationModel model: RCConversationModel!, at indexPath: IndexPath!) {
        
        let chat = ChatViewController()//可以自己创建,也可以写原生控制器RCConversationViewController
        
        chat.conversationType = model.conversationType
        chat.targetId = model.targetId
        
        self .present(chat, animated: true, completion: nil)
    }

点击列表界面头像方法

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

cell即将显示的时候

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

点击头像代理方法

    override func didTapCellPortrait(_ userId: String!) {
        print("%@",userId)
    }

相关文章

网友评论

      本文标题:集成融云即时通讯(Swift)

      本文链接:https://www.haomeiwen.com/subject/kyoyqxtx.html