XuqmGroup-Web/docs-site/docs/ios/index.md
XuqmGroup 277e8ed7c6 feat(im): 添加即时消息SDK核心功能实现
- 实现了聊天消息发送功能,支持文本、图片、视频、音频、文件等多种消息类型
- 集成了文件上传下载功能,支持多媒体文件的传输和管理
- 添加了群组管理功能,包括创建群组、成员管理、权限控制等操作
- 实现了好友系统,支持好友添加、删除、分组等功能
- 集成了黑名单管理,提供用户屏蔽和解除屏蔽功能
- 添加了会话管理功能,支持对话列表、未读消息统计等
- 实现了历史消息查询和搜索功能
- 添加了实时连接状态管理和自动重连机制
2026-05-03 00:11:06 +08:00

6.9 KiB

iOS SDK 概览

版本0.1.0 · 最低 iOS 版本iOS 14 · 语言Swift 5.9+

功能模块

模块 功能
XuqmCore 初始化、网络、鉴权
XuqmIM 单聊、群聊、消息收发15 种类型)、连接状态监听
XuqmPush APNs 设备 Token 注册、FCM 备选、通知处理
XuqmUpdate App 版本检查、App Store 跳转

安装

Swift Package Manager推荐

在 Xcode → File → Add Package Dependencies 中输入:

https://github.com/xuqm/XuqmGroup-iOSSDK

或在 Package.swift 中添加:

dependencies: [
    .package(url: "https://github.com/xuqm/XuqmGroup-iOSSDK", from: "0.1.0")
]

按需引入所需模块:

.target(
    name: "MyApp",
    dependencies: [
        .product(name: "XuqmCore", package: "XuqmGroup-iOSSDK"),
        .product(name: "XuqmIM",   package: "XuqmGroup-iOSSDK"),
        .product(name: "XuqmPush", package: "XuqmGroup-iOSSDK"),
        .product(name: "XuqmUpdate", package: "XuqmGroup-iOSSDK"),
    ]
)

快速接入

1. 初始化

AppDelegate.application(_:didFinishLaunchingWithOptions:) 中:

import XuqmCore

XuqmSDK.shared.initialize(config: config)

2. IM 登录与监听消息UserSig 模式)

import XuqmIM

// 使用 UserSig 登录(推荐生产环境)
// 只需要 userId + userSig,不需要 nickname / avatar / 生命周期字段
try await XuqmSDK.shared.login(userId: "user_001", userSig: "your_user_sig_jwt")

// 设置事件代理
ImSDK.shared.setDelegate(self)

extension ViewController: ImEventDelegate {
    func imClientDidConnect() { print("IM connected") }
    func imClientDidReceiveMessage(_ msg: ImMessage) { /* 处理单聊消息 */ }
    func imClientDidReceiveGroupMessage(_ msg: ImMessage) { /* 处理群消息 */ }
    func imClientDidReadMessage(_ msg: ImMessage) { /* 对方已读回执 */ }
    func imClientDidReceiveRevokedMessage(_ msg: ImMessage) { /* 消息被撤回 */ }
    func imClientDidDisconnect(reason: String?) { /* 断线 */ }
    func imClientDidError(_ error: String) { /* 错误 */ }
}

3. 连接状态监听

SDK 暴露 connectionState 属性与 addConnectionStateListener,方便业务层实时感知 IM 连接状态:

// 查询当前状态
let state = ImSDK.shared.connectionState
// 返回:.disconnected / .connecting / .connected

// 注册状态变更监听器
ImSDK.shared.addConnectionStateListener { state in
    switch state {
    case .connected:
        print("IM 已连接")
    case .connecting:
        print("IM 连接中...")
    case .disconnected:
        print("IM 已断开")
    }
}

状态变更触发时机:

  • 登录后 → .connecting.connected
  • 网络异常/断线 → .disconnected,内部自动重连 → .connecting.connected
  • 调用 disconnect()logout().disconnected

4. 发送消息

// 发文本
let msg = ImSDK.shared.sendTextMessage(
    toId: "user_002",
    chatType: .single,
    content: "Hello!"
)

// 发图片
let imgContent = try JSONSerialization.data(withJSONObject: [
    "url": "https://cdn.example.com/img.jpg",
    "width": 800, "height": 600
])
let msg2 = try await ImSDK.shared.sendMessage(
    toId: "user_002", chatType: .single,
    msgType: .image, content: String(data: imgContent, encoding: .utf8)!
)

5. 撤回与编辑

// 撤回消息
let revoked = try await ImSDK.shared.revokeMessage(messageId: msg.id)

// 编辑消息
let edited = try await ImSDK.shared.editMessage(messageId: msg.id, content: "新内容")

6. 群聊

// 创建群组
let group = try await ImSDK.shared.createGroup(name: "我的群", memberIds: ["user_001", "user_002"])

// 订阅群消息
ImSDK.shared.subscribeGroup(group.id)

// 发送群消息
ImSDK.shared.sendTextMessage(toId: group.id, chatType: .group, content: "大家好")

// 群管理
 try await ImSDK.shared.addGroupMember(groupId: group.id, userId: "user_003")
 try await ImSDK.shared.leaveGroup(groupId: group.id)

7. 会话管理

// 会话列表
let conversations = try await ImSDK.shared.listConversations()

// 置顶 / 静音 / 已读 / 草稿 / 删除
try await ImSDK.shared.setConversationPinned(targetId: "user_002", chatType: .single, pinned: true)
try await ImSDK.shared.setConversationMuted(targetId: "user_002", chatType: .single, muted: true)
try await ImSDK.shared.markRead(targetId: "user_002", chatType: .single)
try await ImSDK.shared.setDraft(targetId: "user_002", chatType: .single, draft: "未完成")
try await ImSDK.shared.deleteConversation(targetId: "user_002", chatType: .single)

8. Push 设备注册APNs + FCM

import XuqmPush

// 请求通知授权并自动注册 APNs
let granted = try await PushSDK.shared.requestAuthorization(options: [.alert, .badge, .sound])

// 在 AppDelegate 中转发 deviceToken
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    XuqmSDK.shared.registerDeviceToken(deviceToken)
}

// 如需手动注册 FCM Token若集成 FirebaseMessaging
try await PushSDK.shared.registerFcmToken(fcmToken, userId: "user_001")

9. 多模块统一登录

无论集成了哪些模块IM、Push、Update初始化和登录永远只做一次

// 初始化
XuqmSDK.shared.initialize(config: config)

// 登录(业务登录成功后调用一次)
try await XuqmSDK.shared.login(userId: "user_001", userSig: userSig)
// ↓ 自动触发
//   · ImSDK 连接 IM
//   · PushSDK 注册 APNs Token 并上报

// 登出(业务退出时调用一次)
XuqmSDK.shared.logout()

注意userSig 由业务服务端签发。若需更新登录态,请直接重新登录。

10. 检查更新

import XuqmUpdate

// App 整包更新
let appInfo = try await UpdateSDK.shared.checkAppUpdate(currentVersionCode: 1)
if let info = appInfo, info.forceUpdate == true {
    UpdateSDK.shared.openAppStore(url: info.appStoreUrl ?? info.downloadUrl!)
}

消息类型

MsgType 说明 content 结构
.text 纯文本 String
.image 图片 {url, width, height, thumbnailUrl?}
.video 视频 {url, duration, thumbnailUrl, size}
.audio 语音 {url, duration, size}
.file 文件 {url, name, size, mimeType}
.location 位置 {lat, lng, address, title}
.custom 自定义 任意 JSON
.notify 系统通知 {title, content}
.richText 富文本 {html}
.callAudio 语音通话信令 {callId, action, callerName}
.callVideo 视频通话信令 {callId, action, callerName}
.forward 转发 {originalMsgId, originalContent, originalSender}
.quote 引用 {quotedMsgId, quotedContent, text}
.merge 合并转发 {title, msgList}
.revoked 撤回 系统内部填充