
新增.gitignore、Podfile和Podfile.lock文件以管理项目依赖,添加README.md文件提供项目简介和安装步骤,创建NIMSessionManager、ClientConfig、LogManager和NetworkManager等管理类以支持网络请求和日志记录功能,更新AppDelegate和ContentView以集成NIM SDK和实现用户登录功能。
128 lines
3.6 KiB
Swift
128 lines
3.6 KiB
Swift
import Foundation
|
|
import NIMSDK
|
|
|
|
// MARK: - 网络状态通知
|
|
extension Notification.Name {
|
|
static let NIMNetworkStateChanged = Notification.Name("NIMNetworkStateChangedNotification")
|
|
static let NIMTokenExpired = Notification.Name("NIMTokenExpiredNotification")
|
|
}
|
|
|
|
@objc
|
|
@objcMembers
|
|
final class NIMSessionManager: NSObject {
|
|
|
|
static let shared = NIMSessionManager()
|
|
|
|
// MARK: - 登录管理
|
|
func autoLogin(account: String, token: String, completion: @escaping (Error?) -> Void) {
|
|
NIMSDK.shared().v2LoginService.add(self)
|
|
let data = NIMAutoLoginData()
|
|
data.account = account
|
|
data.token = token
|
|
data.forcedMode = false
|
|
NIMSDK.shared().loginManager.autoLogin(data)
|
|
}
|
|
|
|
func login(account: String, token: String, completion: @escaping (Error?) -> Void) {
|
|
NIMSDK.shared().loginManager.login(account, token: token) { error in
|
|
if error == nil {
|
|
self.registerObservers()
|
|
}
|
|
completion(error)
|
|
}
|
|
}
|
|
|
|
func logout() {
|
|
NIMSDK.shared().loginManager.logout { _ in
|
|
self.removeObservers()
|
|
}
|
|
}
|
|
|
|
// MARK: - 消息监听
|
|
private func registerObservers() {
|
|
// 在 autoLogin 方法中
|
|
// NIMSDK.shared().v2LoginService.add(self as! V2NIMLoginServiceDelegate)
|
|
|
|
// 在 registerObservers 方法中
|
|
// NIMSDK.shared().v2LoginService.add(self as! V2NIMLoginServiceDelegate)
|
|
|
|
// 在 removeObservers 方法中
|
|
// NIMSDK.shared().v2LoginService.remove(self as! V2NIMLoginServiceDelegate)
|
|
NIMSDK.shared().chatManager.add(self)
|
|
NIMSDK.shared().loginManager.add(self)
|
|
}
|
|
|
|
private func removeObservers() {
|
|
NIMSDK.shared().v2LoginService.remove(self)
|
|
NIMSDK.shared().chatManager.remove(self)
|
|
NIMSDK.shared().loginManager.remove(self)
|
|
}
|
|
}
|
|
|
|
// MARK: - NIMChatManagerDelegate
|
|
extension NIMSessionManager: NIMChatManagerDelegate {
|
|
func onRecvMessages(_ messages: [NIMMessage]) {
|
|
NotificationCenter.default.post(
|
|
name: .NIMDidReceiveMessage,
|
|
object: messages
|
|
)
|
|
}
|
|
}
|
|
|
|
// MARK: - NIMLoginManagerDelegate
|
|
extension NIMSessionManager: NIMLoginManagerDelegate {
|
|
func onLogin(_ step: NIMLoginStep) {
|
|
NotificationCenter.default.post(
|
|
name: .NIMLoginStateChanged,
|
|
object: step
|
|
)
|
|
}
|
|
|
|
func onAutoLoginFailed(_ error: Error) {
|
|
if (error as NSError).code == 302 {
|
|
NotificationCenter.default.post(name: .NIMTokenExpired, object: nil)
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - 通知定义
|
|
extension Notification.Name {
|
|
static let NIMDidReceiveMessage = Notification.Name("NIMDidReceiveMessageNotification")
|
|
static let NIMLoginStateChanged = Notification.Name("NIMLoginStateChangedNotification")
|
|
}
|
|
|
|
// MARK: - NIMV2LoginServiceDelegate
|
|
extension NIMSessionManager: V2NIMLoginListener {
|
|
func onLoginStatus(_ status: V2NIMLoginStatus) {
|
|
|
|
}
|
|
|
|
func onLoginFailed(_ error: V2NIMError) {
|
|
|
|
}
|
|
|
|
func onKickedOffline(_ detail: V2NIMKickedOfflineDetail) {
|
|
|
|
}
|
|
|
|
func onLoginClientChanged(
|
|
_ change: V2NIMLoginClientChange,
|
|
clients: [V2NIMLoginClient]?
|
|
) {
|
|
|
|
}
|
|
// @objc func onLoginProcess(step: NIMV2LoginStep) {
|
|
// NotificationCenter.default.post(
|
|
// name: .NIMV2LoginStateChanged,
|
|
// object: step
|
|
// )
|
|
// }
|
|
//
|
|
// @objc func onKickOut(result: NIMKickOutResult) {
|
|
// NotificationCenter.default.post(
|
|
// name: .NIMKickOutNotification,
|
|
// object: result
|
|
// )
|
|
// }
|
|
}
|