In iOS 14, It could display ATT (App Tracking Transparency) dialog when app starts in SwiftUI as follows.
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
}
But, in iOS 15.0, it does not work. Apple document describes as follows.
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
How to display ATT dialog when the app starts in iOS 15 ?
2021/9/28 update I solved it as follows.
struct HomeView: View {
var body: some View {
VStack {
Text("Hello!")
}.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
ATTrackingManager.requestTrackingAuthorization(completionHandler: { status in })
}
}
}