swift iOS上未显示应用程序跟踪透明度对话框

gijlo24d  于 2022-12-10  发布在  Swift
关注(0)|答案(5)|浏览(311)

由于ATT请求未出现,Apple评论者刚刚拒绝了我的应用程序:“在iOS 15.0.1上查看时,我们无法找到应用程序跟踪透明度权限请求。”
我的代码如下:

if #available(iOS 14, *) {
  ATTrackingManager.requestTrackingAuthorization { (status) in
    //print("IDFA STATUS: \(status.rawValue)")
    FBAdSettings.setAdvertiserTrackingEnabled(true)
  }
}

我在AppDelegate didFinishLaunchingWithOptionsviewDidLoad中实现了这段代码。ATT权限请求出现在iOS 14中,但不在iOS 15中。

bjp0bcyl

bjp0bcyl1#

该死的,我修复了它:(这都是关于iOS警报系统的。我在请求通知请求后请求应用程序跟踪透明度。一旦通知请求警报关闭,ATT警报需要出现。它在iOS 14上工作正常,但在iOS 15上显示一个警报后,另一个警报,它需要有一个彼此之间的延迟期。
下面是我的代码,分别显示两个警报:

func setNotification(){
    //Ask for notification permission
    let n = NotificationHandler()
    n.askNotificationPermission {
        //n.scheduleAllNotifications()
        
        //IMPORTANT: wait for 1 second to display another alert
        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
            if #available(iOS 14, *) {
              ATTrackingManager.requestTrackingAuthorization { (status) in
                //print("IDFA STATUS: \(status.rawValue)")
                //FBAdSettings.setAdvertiserTrackingEnabled(true)
              }
            }
        }
    }
}

为了方便起见,下面是我的NotificaitionHandler类:

import UserNotifications

class NotificationHandler{
//Permission function
func askNotificationPermission(completion: @escaping ()->Void){
    
    //Permission to send notifications
    let center = UNUserNotificationCenter.current()
    // Request permission to display alerts and play sounds.
    center.requestAuthorization(options: [.alert, .badge, .sound])
    { (granted, error) in
        // Enable or disable features based on authorization.
        completion()
    }
}
ycl3bljg

ycl3bljg2#

一个Apple person建议你通过AppDelegate中的applicationDidBecomeActive(_:)请求它。这就是我如何修复iOS的问题。

import UIKit
import AppTrackingTransparency

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
    func applicationDidBecomeActive(_ application: UIApplication) {
        if #available(iOS 15.0, *) {
            ATTrackingManager.requestTrackingAuthorization(completionHandler: { status in
                
            })
        }
    }
}
ih99xse1

ih99xse13#

我已更改为调用来自

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
}

@available(iOS 13.0, *)
func sceneDidBecomeActive(_ scene: UIScene) {
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
        self.requestPermission()
    }
}

func requestPermission() {
    if #available(iOS 15.0, *) {
            ATTrackingManager.requestTrackingAuthorization(completionHandler: { status in
                switch status {
                case .authorized:
                    // Tracking authorization dialog was shown
                    // and we are authorized
                    print("Authorized")
                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:
                   
                }
            })
        }
    }
}
rhfm7lfc

rhfm7lfc4#

我已经更改为从AppDelegate的applicationDidBecomeActive方法调用请求,并且成功了!
在iOS 15上首次启动应用程序时会显示弹出窗口。

vql8enpb

vql8enpb5#

以下是当应用程序处于活动状态时如何在SwiftUI中调用请求:

@main
struct MyApp: App {
    @Environment(\.scenePhase) var scenePhase
    
    var body: some Scene {
        WindowGroup {
            MainView()
                .onChange(of: scenePhase, perform: { newValue in
                    if newValue == .active {
                        ATTrackingManager.requestTrackingAuthorization { status in
                            // do something
                        }
                    }
                })
        }
    }
}

相关问题