Reputation: 176
I am trying to implement App Tracking Transparency in my app. I tried to do this on first ViewControllor, but it crashes the app after uploading to the test flight.
After this, I found a lot of info that this should be done in appDelegate I did this way. Of course, I have set NSUserTrackingUsageDescription in Info.plist
I tried to figure it out with this post.
In the debugger, I always see "Not Determined". Could anyone please help with this?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
requestTrackingPermission()
GADMobileAds.sharedInstance().start(completionHandler: nil)
return true
}
The function
func requestTrackingPermission() {
if #available(iOS 14, *) {
// ATTrackingManager.requestTrackingAuthorization { status in
ATTrackingManager.requestTrackingAuthorization(completionHandler: { status in
switch status {
case .authorized:
// Tracking authorization dialog was shown
// and we are authorized
print("Authorized")
// Now that we are authorized we can get the IDFA
print(ASIdentifierManager.shared().advertisingIdentifier)
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")
}
}
)}
}
Upvotes: 3
Views: 1750
Reputation: 176
I found how to do this.
Add in your first ViewController
import AppTrackingTransparency
import AdSupport
import UserNotifications
Add in viewDidLoad
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
if #available(iOS 14, *) {
ATTrackingManager.requestTrackingAuthorization { (status) in
print("IDFA STATUS: \(status.rawValue)")
}
}
}
Upvotes: 4
Reputation: 1
Nah forget all that. It took me forever to find. But you don't need to trick it by having a delay. You just need to show xcode the app is active. Just copy and paste this:
NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: nil) { _ in self.requestIDFA() }
return true
}
func requestIDFA() {
ATTrackingManager.requestTrackingAuthorization { _ in }
Upvotes: -1