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