iOS SDK - 卸载追踪

卸载追踪

通过将 Apple Push Notification Service (APNs) 与 Singular SDK 集成, 追踪应用卸载情况,以衡量用户留存并优化重新激活活动。

重要: 卸载追踪需要在应用中配置 APNs。有关完整的实施详情,请参阅 Apple 的 UserNotifications Framework 文档

前提条件

配置 Singular 平台

在应用中实施卸载追踪之前,请按照以下指南在 Singular 平台中配置您的应用 设置 iOS 卸载追踪


系统要求

卸载追踪需要 Apple Push Notification Service 和特定的设备配置。

APNs 要求 ( 来源 ):

  • iOS 版本: 设备必须运行 iOS 10.0 或更高版本以支持 UserNotifications 框架
  • APNs 证书: 在 Apple Developer 门户中配置 APNs 证书或 token
  • 推送功能: 在 Xcode 项目设置中启用 Push Notifications 权限
  • 用户权限: 请求并获得用户对推送通知的授权

注意: 拒绝推送通知权限或使用不支持 APNs 的设备的用户将不会被纳入卸载追踪。


实施步骤

步骤 1: 启用推送通知

如果尚未启用,请配置您的 Xcode 项目以支持推送通知。

  1. 在 Xcode 中打开您的项目
  2. 选择您的应用 target 并进入 Signing & Capabilities
  3. 点击 + Capability 并添加 Push Notifications
  4. 确保在 Background Modes 中勾选 Remote notifications

步骤 2: 请求用户授权

使用 UserNotifications 框架请求用户接收推送通知的权限。

Swift Objective-C
import UIKit
import UserNotifications

class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication,
                    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        // Set notification delegate
        UNUserNotificationCenter.current().delegate = self

        // Request authorization for notifications
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { granted, error in
            if let error = error {
                print("Notification authorization error: \(error.localizedDescription)")
            }

            if granted {
                // Register for remote notifications on main thread
                DispatchQueue.main.async {
                    application.registerForRemoteNotifications()
                }
            }
        }

        return true
    }
}

步骤 3: 注册 APNs 设备 token

获取 APNs 设备 token 并将其发送至 Singular 以进行卸载追踪。

获取并设置 token

实现 didRegisterForRemoteNotificationsWithDeviceToken 回调以捕获 APNs token 并将其注册到 Singular。

Swift Objective-C
import Singular

func application(_ application: UIApplication,
                didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

    // Register the device token with Singular for uninstall tracking
    Singular.registerDeviceToken(forUninstall: deviceToken)

    print("APNs device token registered with Singular")

    // Also send to your server if needed for your own push notifications
    sendTokenToServer(deviceToken)
}

func application(_ application: UIApplication,
                didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print("Failed to register for remote notifications: \(error.localizedDescription)")
}

private func sendTokenToServer(_ deviceToken: Data) {
    // Implement your server communication logic here
}

最佳实践: 收到 token 后立即调用 registerDeviceTokenForUninstall ,以确保从第一次应用会话开始就启用卸载追踪。


Token 数据格式

将 APNs 设备 token 按从 Apple 接收到的原生二进制格式传递给 Singular。

重要: APNs token 是二进制数据 (NSData/Data)。请直接将其传递给 Singular,无需转换。 如果您的应用需要将 token 转换为十六进制字符串用于其他用途,请为 Singular 保留原始 NSData 对象。

十六进制格式示例: b0adf7c9730763f88e1a048e28c68a9f806ed032fb522debff5bfba010a9b052


验证与故障排查

验证实施

确认卸载追踪是否正常工作。

  1. 检查日志: 验证 APNs token 注册是否出现在您的日志中
  2. 测试 token 生成: 确保 token 在应用首次启动时生成
  3. 监控仪表板: 在 24-48 小时后检查 Singular 仪表板中的卸载追踪数据
  4. 测试权限: 验证通知授权提示是否出现并正常工作

常见问题

  • Token 未生成: 验证 Xcode 中是否已启用 Push Notifications 权限, 并且 APNs 证书已在 Apple Developer 门户中配置
  • 用户拒绝了权限: 实现相应逻辑以妥善处理被拒绝的通知权限
  • 数据缺失: 确保设备运行 iOS 10.0 或更高版本,且用户已授予通知权限
  • 配置错误: 确认 Singular 平台设置中已启用卸载追踪
  • 模拟器限制: APNs token 在 iOS 模拟器中不可用;请在实体设备上测试

其他资源: 有关详细的故障排查,请参阅 卸载追踪设置指南 Apple 的 UserNotifications 文档