iOS SDK
AntDock iOS SDK 使用 Swift 编写,支持 iOS 12.0+。
SDK 获取方式
使用控制台提供的安装地址或下载包;本文不表示 SDK 已在公共仓库开源发布。
安装
将 SDK 源码文件复制到您的 iOS 项目中:
- 下载
sdk/ios/Sources/AntDockTracker/目录下的所有 Swift 文件 - 将文件添加到 Xcode 项目中
- 确保文件被包含在 Target 中
初始化
在 AppDelegate.swift 中初始化:
import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
AntDockTracker.shared.initialize(
appId: "your-app-id",
endpoint: "https://你的域名",
options: AntDockOptions(
autoTrack: true,
batchSize: 10,
flushInterval: 5.0,
debug: false
)
)
return true
}
}
SwiftUI App
import SwiftUI
@main
struct MyApp: App {
init() {
AntDockTracker.shared.initialize(
appId: "your-app-id",
endpoint: "https://你的域名"
)
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
API 参考
trackPageView(_:properties:)
追踪页面访问。
AntDockTracker.shared.trackPageView("/home")
AntDockTracker.shared.trackPageView("/product/detail", properties: [
"product_id": "12345"
])
trackEvent(_:properties:)
追踪自定义事件。
AntDockTracker.shared.trackEvent("button_click")
AntDockTracker.shared.trackEvent("purchase", properties: [
"order_id": "ORD-12345",
"amount": 99.99
])
identify(_:traits:)
识别用户身份。
AntDockTracker.shared.identify(userId: "user_123", traits: [
"email": "user@example.com",
"name": "张三"
])
flush()
立即发送队列中的数据。
reset()
重置用户身份。
UIKit 集成
class ProductDetailViewController: UIViewController {
var productId: String = ""
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
AntDockTracker.shared.trackPageView("/product/\(productId)")
}
}
SwiftUI 集成
struct ProductDetailView: View {
let productId: String
var body: some View {
VStack {
// 内容...
}
.onAppear {
AntDockTracker.shared.trackPageView("/product/\(productId)")
}
}
}
配置选项
AntDockOptions(
autoTrack: true, // 自动追踪
enableCrashReport: false, // 崩溃上报
batchSize: 10, // 批量大小
flushInterval: 5.0, // 刷新间隔 (秒)
debug: false // 调试模式
)
要求
- iOS 12.0+
- Swift 5.5+