我们可以检查允许应用程序请求跟踪切换是否打开

如何解决我们可以检查允许应用程序请求跟踪切换是否打开

在 iOS 14 中,我们有一项新功能可以跟踪 IDFA。这是认设置,仅在 iOS 14 中可用。(设置 > 隐私 > 跟踪 > 允许应用请求跟踪)。我想使用objective-C检查允许应用程序请求跟踪切换是打开还是关闭。我该怎么做?

enter image description here

解决方法

请注意,我们只能使用 ATTrackingManager API 访问我们自己的应用的授权状态。我们无法使用任何公共 API 读取全局设置。

您可以通过检查 [ATTrackingManager trackingAuthorizationStatus] 的值来检查应用的状态:

ATTrackingManagerAuthorizationStatus status = [ATTrackingManager trackingAuthorizationStatus];
switch (status) {
    case ATTrackingManagerAuthorizationStatusNotDetermined:
        // The user has not yet received an authorization request to authorize access to app-related data that can be used for tracking the user or the device.
        break;
    case ATTrackingManagerAuthorizationStatusAuthorized:
        // The user authorizes access to app-related data that can be used for tracking the user or the device.
        break;
    case ATTrackingManagerAuthorizationStatusDenied:
        // The user denies authorization to access app-related data that can be used for tracking the user or the device.
        break;
    case ATTrackingManagerAuthorizationStatusRestricted:
        // The authorization to access app-related data that can be used for tracking the user or the device is restricted.
        break;
}

您也可以在向用户请求授权后读取相同的状态:

[ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
    // Check for status after request
}];

另请注意,我们只能请求一次授权。不建议一直向用户索要请求。
但是,如果您的应用确实需要这样做,那么解决方案是将用户导航到“设置”,指示他们为应用打开授权:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [self checkTrackingAuthorization:ATTrackingManager.trackingAuthorizationStatus];
}

- (void)checkTrackingAuthorization:(ATTrackingManagerAuthorizationStatus) status {
    switch (status) {
        case ATTrackingManagerAuthorizationStatusAuthorized:
            // The user authorizes access to app-related data that can be used for tracking the user or the device.
            break;
        case ATTrackingManagerAuthorizationStatusNotDetermined:
            // The user has not yet received an authorization request to authorize access to app-related data that can be used for tracking the user or the device.
            [self requestTrackingAccess];
            break;
        case ATTrackingManagerAuthorizationStatusDenied:
            // The user denies authorization to access app-related data that can be used for tracking the user or the device.
        case ATTrackingManagerAuthorizationStatusRestricted:
            // The authorization to access app-related data that can be used for tracking the user or the device is restricted.
            [self displayTrackingAccessAlert];
            break;
    }
}

- (void)requestTrackingAccess {
    [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
        [self checkTrackingAuthorization:status];
    }];
}

- (void)displayTrackingAccessAlert {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Tracking access is required" message:@"Please turn on access to tracking on the settings" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *settingsAction = [UIAlertAction actionWithTitle:@"Settings" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // Open the Settings app
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:nil];
    
    [alert addAction:settingsAction];
    [alert addAction:cancelAction];
    [alert setPreferredAction:settingsAction];
    
    UIWindow *alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    alertWindow.rootViewController = [[UIViewController alloc] init];
    alertWindow.windowLevel = UIWindowLevelAlert + 1;
    [alertWindow makeKeyAndVisible];
    [alertWindow.rootViewController presentViewController:alert animated:YES completion:nil];
}
,

您需要跟踪您的应用是否已请求跟踪权限(例如,将 bool 存储在 NSUserDefaults 中。

然后您可以检查[ATTrackingManager trackingAuthorizationStatus]。如果状态为 ATTrackingManagerAuthorizationStatusDenied 但您从未请求过跟踪权限(因此用户不能明确拒绝对您的应用的跟踪权限),您知道“允许应用请求跟踪”开关是关闭。

一旦您请求许可,您就无法再确定开关是否已关闭,或者用户是否刚刚明确拒绝了对您的应用的跟踪权限。无论哪种方式,您都无能为力,并且不断纠缠用户打开跟踪功能不会让他们喜欢您的应用。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?