【问题标题】:ATTrackingManager stopped working in iOS 15ATTrackingManager 在 iOS 15 中停止工作
【发布时间】:2021-11-17 14:21:12
【问题描述】:

ATTrackingManager.requestTrackingAuthorization 在 ios 15 上停止工作。应用程序被 Apple 拒绝。

【问题讨论】:

标签: swift ios15


【解决方案1】:

根据苹果开发者论坛的讨论,调用requestTrackingAuthorization的时候需要加延迟一秒左右。 https://developer.apple.com/forums/thread/690607

例子:

DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
          ATTrackingManager.requestTrackingAuthorization(completionHandler: { status in
        // Tracking authorization completed. Start loading ads here.
        // loadAd()
      })
})

附: 此外,如果您有请求推送通知权限,首先您需要请求推送通知,然后请求延迟跟踪授权 =>

    private func requestPushNotificationPermission() {
    let center = UNUserNotificationCenter.current()
    UNUserNotificationCenter.current().delegate = self
    center.requestAuthorization(options: [.sound, .alert, .badge], completionHandler: { (granted, error) in
        if #available(iOS 14.0, *) {
            DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
                ATTrackingManager.requestTrackingAuthorization(completionHandler: { status in
                    // Tracking authorization completed. Start loading ads here.
                    // loadAd()
                })
            })
        }})
    UIApplication.shared.registerForRemoteNotifications()
}

【讨论】:

    【解决方案2】:

    问题已经解决了,直接在applicationDidBecomeActive调用即可: https://developer.apple.com/forums/thread/690762

    【讨论】:

    【解决方案3】:

    确保您的 iPhone 设置 -> 隐私 -> 跟踪已启用。否则,它不会提示请求授权。

    【讨论】:

      【解决方案4】:

      关注苹果文档:

      仅当应用程序状态为 UIApplicationStateActive 时才提示调用 API。

      所以,我们需要打电话给ATTrackingManager.requestTrackingAuthorization applicationDidBecomeActiveAppDelegate

      但是如果你使用场景(参见场景),UIKit 不会调用这个方法。请改用sceneDidBecomeActive(_:) 来重新启动任何任务或刷新应用的用户界面。无论您的应用是否使用场景,UIKit 都会发布didBecomeActiveNotification

      所以,我的做法是在addObserver上注册didFinishLaunchingWithOptions如:

      NotificationCenter.default.addObserver(self, selector: #selector(handleRequestEvent), name: UIApplication.didBecomeActiveNotification, object: nil)

      handleRequestEvent:

      requestPermission() // func call ATTrackingManager.requestTrackingAuthorization NotificationCenter.default.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)

      希望这会有所帮助。这对我有用。

      【讨论】:

        猜你喜欢
        • 2021-11-17
        • 2017-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多