gpt4 book ai didi

iOS 15 : How to display ATT dialog when the app starts in SwiftUI

转载 作者:行者123 更新时间:2023-12-04 12:14:52 26 4
gpt4 key购买 nike

在 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 中,它不起作用。 Apple 文档描述如下。

Calls to the API only prompt when the application state is: UIApplicationStateActive. Calls to the API through an app extension do not prompt.https://developer.apple.com/documentation/apptrackingtransparency/attrackingmanager/3547037-requesttrackingauthorization


当应用程序在 iOS 15 中启动时如何显示 ATT 对话框?
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 })
}
}
}

最佳答案

而不是在 中调用应用跟踪透明度权限didFinishLaunchingWithOptions 调用 applicationDidBecomeActive 它会解决你的问题
AppDelegate

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
}
}
info.plist
<key>NSUserTrackingUsageDescription</key>
<string>Reason for data tracking</string>

关于iOS 15 : How to display ATT dialog when the app starts in SwiftUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69283661/

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