ATrackingManager 在 iOS 15 中停止工作

Taj*_*ngh 20 swift ios15

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

小智 29

根据苹果开发者论坛的讨论,调用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()
      })
})
Run Code Online (Sandbox Code Playgroud)

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

    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()
}
Run Code Online (Sandbox Code Playgroud)


Taj*_*ngh 10

问题已经解决,只需调用它即可applicationDidBecomeActivehttps ://developer.apple.com/forums/thread/690762


Tus*_*iya 10

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


小智 10

按照苹果文档:

\n
\n

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

\n
\n

因此,我们需要调用ATTrackingManager.requestTrackingAuthorization\n applicationDidBecomeActiveof AppDelegate

\n
\n

但如果您使用场景(请参阅场景),UIKit 将不会调用此方法。请sceneDidBecomeActive(_:)改为重新启动任何任务或刷新您的 app\xe2\x80\x99s 用户界面。didBecomeActiveNotification无论您的应用程序是否使用场景,UIKit 都会发布一个。

\n
\n

因此,我的方法是addObserver注册didFinishLaunchingWithOptions

\n

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

\n

handleRequestEvent

\n

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

\n

希望这可以帮助。这对我来说是工作。

\n