30

我正在研究如何在 iOS 14 上获取 IDFA。我正在使用 iPhone 8 Plus。

我已经添加了

<key>NSUserTrackingUsageDescription</key>
<string>App would like to access IDFA for tracking purpose</string>

.plist文件中。

然后添加

let type = ATTrackingManager.trackingAuthorizationStatus;

返回.denied,有

func requestPermission() {
        ATTrackingManager.requestTrackingAuthorization { status in
            switch status {
            case .authorized:
                // Tracking authorization dialog was shown
                // and we are authorized
                print("Authorized")
            
                // Now that we are authorized we can get the IDFA
            print(ASIdentifierManager.shared().advertisingIdentifier)
            case .denied:
               // Tracking authorization dialog was
               // shown and permission is denied
                 print("Denied")
            case .notDetermined:
                    // Tracking authorization dialog has not been shown
                    print("Not Determined")
            case .restricted:
                    print("Restricted")
            @unknown default:
                    print("Unknown")
            }
        }
    }

但我.denied没有任何弹出窗口。

你知道发生了什么吗?

4

8 回答 8

32

在系统的设置应用中有一个“允许应用请求跟踪”选项,如果它关闭,requestTrackingAuthorization.denied立即返回。

隐私设置
于 2020-07-10T18:40:08.640 回答
15

在 iOS 15 中,仅当应用程序状态已处于活动状态时才可以使用 ATTrackingManager.requestTrackingAuthorization 请求,因此应从 didFinishLaunchingWithOptions 移动到 applicationDidBecomeActive

于 2021-09-29T09:09:16.313 回答
4

如果全局设置Allow Apps to Request to Track为 OFF,requestTrackingAuthorization.denied立即返回。

但是对于某些用户,即使在允许应用程序请求跟踪为 ON 之后,requestTrackingAuthorization正在返回.denied

这是在 14.5.1 版本中修复的操作系统问题,因此只需更新您的操作系统以获取 ATT 对话框。

iOS 和 iPadOS 14.5.1 发行说明

此更新修复了 App Tracking Transparency 的问题,即之前在“设置”中禁用“允许应用请求跟踪”的某些用户在重新启用后可能不会收到来自应用的提示。此更新还提供重要的安全更新,建议所有用户使用。

于 2021-05-04T05:17:54.413 回答
2

SwiftUI iOS 15+

要为您显示警报,ATTrackingManager.requestTrackingAuthorization必须确保在应用程序处于活动状态时调用它(否则警报不会显示)

为此,我正在使用 Publisher 进行检查

ContentView()
            .onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
                if ATTrackingManager.trackingAuthorizationStatus == .notDetermined {
                    ATTrackingManager.requestTrackingAuthorization { status in
                        //Whether or not user has opted in initialize GADMobileAds here it will handle the rest
                                                                    
                        GADMobileAds.sharedInstance().start(completionHandler: nil)
                        }
                } else {
                    GADMobileAds.sharedInstance().start(completionHandler: nil)
                    
                }
于 2021-12-29T18:47:03.873 回答
1

因为权限被用户拒绝

您可以通过我的代码将用户移动到应用程序的设置页面

UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
于 2020-08-24T09:56:48.523 回答
-1

我的愚蠢问题,你真的在​​调用这个函数吗?请记住,一旦被回答,它就不会再次出现。您必须删除并安装该应用程序才能再次展示它

于 2020-07-10T23:54:36.673 回答
-1

对授权方法 ATTrackingManager.requestTrackingAuthorization 的调用必须放在主控制器的 viewDidAppear(_:) 方法中甚至更远(稍后运行),即可以绑定到按钮

于 2022-02-09T10:53:43.427 回答
-3

看来您需要使用 iOS 14.4。

我假设您之前已经设置了一次允许/拒绝。我遇到了同样的问题,即使在我尝试使用 iOS 14.0 时卸载应用程序,询问权限警报也不会再次出现。但是当我使用 iOS 到 14.4 时,我可以通过卸载我的应用程序来重置设置并再次看到询问警报。

于 2021-01-29T09:28:44.130 回答