gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:08:35 57 4
gpt4 key购买 nike

在 iOS 14 中,我们有一个新功能来跟踪 IDFA。这是默认设置,仅在 iOS 14 中可用。(设置 > 隐私 > 跟踪 > 允许应用请求跟踪)。我想使用 Objective-C 检查允许应用程序请求跟踪切换是打开还是关闭。我怎样才能做到这一点? enter image description here

最佳答案

请注意,我们只能使用 ATTrackingManager API 访问我们自己的应用的授权状态。我们无法使用任何公共(public) 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];
}

关于ios - 我们可以检查允许应用程序请求跟踪切换是否打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65947972/

57 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com