iOS 15:如何在SwiftUI中启动应用程序时显示ATT对话框

xytpbqjk  于 2023-03-05  发布在  iOS
关注(0)|答案(5)|浏览(204)

在iOS 14中,当应用程序在SwiftUI中启动时,它可以显示ATT(应用程序跟踪透明度)对话框,如下所示。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    if #available(iOS 14, *) {
        ATTrackingManager.requestTrackingAuthorization(completionHandler: { status in
            // loadAd()
        })
    } else {
        // loadAd()
    }
    return true
}

但是,在iOS 15.0中,它不工作。苹果文档描述如下。
仅当应用程序状态为以下情况时,才会提示调用API:UIApplicationStateActive。通过应用扩展调用API时不提示。https://developer.apple.com/documentation/apptrackingtransparency/attrackingmanager/3547037-requesttrackingauthorization
如何显示ATT对话框时,应用程序在iOS 15启动?

2021/9/28更新我是这样解决的。

struct HomeView: View {
    var body: some View {
        VStack {
            Text("Hello!")
        }.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
            ATTrackingManager.requestTrackingAuthorization(completionHandler: { status in })
        }
    }
}
0s7z1bwu

0s7z1bwu1#

无需在didFinishLaunchingWithOptions中调用应用跟踪透明度权限,而是在applicationDidBecomeActive中调用,这样就可以解决问题
应用委托

func applicationDidBecomeActive(_ application: UIApplication) {
    requestDataPermission()
}

func requestDataPermission() {
    if #available(iOS 14, *) {
        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:
                print("Unknown")
            }
        })
    } else {
        //you got permission to track, iOS 14 is not yet installed
    }
}

信息列表

<key>NSUserTrackingUsageDescription</key>
<string>Reason_for_data_tracking</string>
g6baxovj

g6baxovj2#

如@donchan所述,使用以下代码:

struct HomeView: View {
    var body: some View {
        VStack {
            Text("Hello!")
        }.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
            ATTrackingManager.requestTrackingAuthorization(completionHandler: { status in })
        }
    }
}
jhdbpxl9

jhdbpxl93#

对于iOS 15,我遇到了同样的问题,我通过延迟代码执行一秒钟来修复它们。

DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
        if #available(iOS 14, *) {
            ATTrackingManager.requestTrackingAuthorization(completionHandler: { status in
                DispatchQueue.main.async {
                    self.bannerView.load(GADRequest())
                    self.interstitial.load(request)
                }
            })
        } else {
            // Fallback on earlier versions
            self.bannerView.load(GADRequest())
            self.interstitial.load(request)
        }
    }
sg3maiej

sg3maiej4#

一个非常重要的补充,所有以上的答案:ATT对话框必须被调用一次!2例如,在广告管理器中,如果你在请求广告之前重复调用ATT对话框(就像以前的操作系统版本一样),那么对话框将不会显示!3因此,ATT对话框请求必须直接插入视图中,并且在无条件触发时延迟至少1秒。

pcww981p

pcww981p5#

如果您正在编写SwiftUI应用程序,则可以在开始屏幕上触发它。

struct HomeView: View {
    
    var body: some View {
        VStack {
            Text("Hello!")
        }.onAppear {
            ATTrackingManager.requestTrackingAuthorization(completionHandler: { status in })
        }
    }
}

不要忘记在. plist中添加必要的内容。

<key>NSUserTrackingUsageDescription</key>
<string>...</string>

因此,它将在模拟器或真实的设备上运行。

相关问题