【发布时间】:2021-11-17 14:21:12
【问题描述】:
ATTrackingManager.requestTrackingAuthorization 在 ios 15 上停止工作。应用程序被 Apple 拒绝。
【问题讨论】:
-
是的,这个问题已经解决了。请点击以下两个链接。 developer.apple.com/forums/thread/690607developer.apple.com/forums/thread/690762
ATTrackingManager.requestTrackingAuthorization 在 ios 15 上停止工作。应用程序被 Apple 拒绝。
【问题讨论】:
根据苹果开发者论坛的讨论,调用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()
}
【讨论】:
问题已经解决了,直接在applicationDidBecomeActive调用即可:
https://developer.apple.com/forums/thread/690762
【讨论】:
确保您的 iPhone 设置 -> 隐私 -> 跟踪已启用。否则,它不会提示请求授权。
【讨论】:
关注苹果文档:
仅当应用程序状态为
UIApplicationStateActive时才提示调用 API。
所以,我们需要打电话给ATTrackingManager.requestTrackingAuthorization
applicationDidBecomeActive 的 AppDelegate。
但是如果你使用场景(参见场景),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)
希望这会有所帮助。这对我有用。
【讨论】: