AppTransparencyTracking在Swift UI中不起作用



由于SwiftUI没有Appdelegate文件,我尝试通过App.swift文件添加它。

然而,它仍然不起作用。我错过了什么?

导入库

import AppTrackingTransparency
import AdSupport
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {func requestIDFA() {
ATTrackingManager.requestTrackingAuthorization(completionHandler: { status in
// Tracking authorization completed. Start loading ads here.
// loadAd()
})
}

然后调用@main 下的appdelegate

@main
struct MyApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

如果您查看didFinishLaunchingWithOptions函数。您在另一个函数中有一个调用函数,因此requestIDFA永远不会调用。

将您的requestIDFA()函数放在didFinishLaunchingWithOptions之外,并在didFinishLaunchingWithOptions内部调用。

class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
requestIDFA()
return true
}

func requestIDFA() {
ATTrackingManager.requestTrackingAuthorization(completionHandler: { status in
// Tracking authorization completed. Start loading ads here.
// loadAd()
})
}
}

注意:确保您在Info.plist.中添加了一个密钥

<key>NSUserTrackingUsageDescription</key>
<string>Your reason, why you want to track the user</string>

最新更新