1. 添加依赖
方式一:Swift Package Manager(推荐)
在 Xcode 中:File → Add Packages,输入仓库地址:
https://github.com/mc-serve/mc-gateway.git
方式二:CocoaPods
# Podfile
pod 'UmcUgateway', '~> 1.0.0'
然后运行:
pod install
方式三:手动集成
- 下载 UmcUgateway.xcframework.zip
- 解压并拖入 Xcode 项目
- 在 Target → General → Frameworks 中添加
2. 初始化 SDK
在 AppDelegate 中初始化:
import UmcUgateway
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let config = UmcUgatewayConfig(
appId: "your_app_id",
serverUrl: "https://api.example.com",
debugMode: true
)
UmcUgateway.shared.initialize(config: config)
return true
}
}
SwiftUI App
import SwiftUI
import UmcUgateway
@main
struct MyApp: App {
init() {
let config = UmcUgatewayConfig(
appId: "your_app_id",
serverUrl: "https://api.example.com"
)
UmcUgateway.shared.initialize(config: config)
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
3. 基本使用
// 手动触发采集和上报
Task {
do {
try await UmcUgateway.shared.collectAndReport()
print("上报成功")
} catch {
print("上报失败: \(error)")
}
}
// 获取本地缓存的数据
let cachedData = UmcUgateway.shared.getCachedData()
4. 隐私权限
根据采集内容,可能需要在 Info.plist 中添加:
<key>NSLocationWhenInUseUsageDescription</key>
<string>用于获取位置信息</string>
注意:SDK 最低支持 iOS 13.0,使用 Swift 5.0 编写。