iOS UIKit

最近更新时间:2026-05-06 17:29:22

我的收藏

准备工作

1. 您需要一个 iOS 应用,这个应用可以是您现有的工程,也可以是您新建的一个空的工程。
2. 请确保您的 iOS 应用版本为 iOS 12 或更高版本。

操作步骤

步骤1:添加 Pod 依赖

1. 从 cnb 根目录下 ios 目录下载 smh uikit 的 Pod 产物所有文件,放到项目中。
2. Podfile 中添加:
target 'YourApp' do
use_frameworks!
platform :ios, '12.0'

// smhuikit 为 smh uikit 的 Pod 产物
pod 'QCloudSMHUIKit', :path => '../../smhuikit'
end
3. 执行安装:
pod install

步骤2:打开文件管理页面

参考如下代码即可打开文件管理页面
Swift 示例:
import QCloudSMHUIKit

// 1. 配置
let config = SMHConfig(
host: "https://xxx.api.tencentsmh.cn",
libraryId: "your-library-id",
spaceId: "your-space-id",
downloadPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! + "/SMHDownload",
identifier: "identifier"
)

// 2. 实现 Token 提供者
class MyTokenProvider: NSObject, SMHAccessTokenProvider {
func getAccessToken() -> SMHAccessToken? {
let result = YourApi.fetchSmhTokenSync()
return SMHAccessToken(
token: result.token,
startTime: Int64(Date().timeIntervalSince1970 * 1000),
expiresIn: result.expiresIn
)
}
}

// 3. 打开文件管理页面
let vc = SMHUIKitSDK.fileListViewController(
with: config,
accessTokenProvider: MyTokenProvider()
)
navigationController?.pushViewController(vc, animated: true)
SwiftUI 示例:
import SwiftUI
import QCloudSMHUIKit

struct SmhFileView: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> UINavigationController {
let config = SMHConfig(
host: "https://xxx.api.tencentsmh.cn",
libraryId: "your-library-id",
spaceId: "your-space-id",
downloadPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! + "/SMHDownload",
identifier: "identifier"
)
let vc = SMHUIKitSDK.fileListViewController(
with: config,
accessTokenProvider: MyTokenProvider()
)
return UINavigationController(rootViewController: vc)
}

func updateUIViewController(_ uiViewController: UINavigationController, context: Context) {}
}

SmhConfig 参数

参数
说明
类型
是否必填
host
SMH API 地址,如 https://xxx.api.tencentsmh.cn
String
libraryId
媒体库 ID
String
spaceId
空间 ID
String
downloadPath
默认下载根路径
String
identifier
业务层标识,一般用于不同业务的 SDK 层数据隔离
String
path
初始 space 目录路径,默认根目录
String
userId
用户 ID(SMH 服务端概念)
String

AccessToken 参数

参数
说明
类型
是否必填
token
访问凭证
String
startTime
Token 起始时间(毫秒时间戳)
Int64
expiresIn
有效时长(秒)
Int
说明:
startTimeexpiresIn 用于描述 Token 有效期。SDK 内部会优先复用未过期 Token,接入方无需额外管理 Token 生命周期。

权限说明

Info.plist 中添加以下权限描述:
<key>NSPhotoLibraryUsageDescription</key>
<string>用于从相册选择图片上传文件</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>用于将下载的图片或视频保存到相册</string>