Created
September 26, 2020 07:21
-
-
Save xavierLowmiller/a72bde62c9ed0a2a382434c6369ca3a9 to your computer and use it in GitHub Desktop.
Experiments using iOS 14's new AppTrackingTransparency framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import AdSupport | |
import AppTrackingTransparency | |
struct ContentView: View { | |
@State private var trackingStatus: String = "" | |
@State private var idfa: String = "" | |
func updateIdfa() { | |
// Keep in mind that this is never valid on a Simulator | |
idfa = ASIdentifierManager.shared().advertisingIdentifier.description | |
} | |
func askUserForTracking() { | |
ATTrackingManager.requestTrackingAuthorization { status in | |
trackingStatus = trackingStatusDescription(status) | |
updateIdfa() | |
} | |
} | |
var body: some View { | |
VStack { | |
Text(trackingStatus) | |
Text(idfa) | |
Button("Ask User about it") { | |
askUserForTracking() | |
} | |
}.onAppear { | |
trackingStatus = trackingStatusDescription() | |
updateIdfa() | |
} | |
} | |
} | |
private func trackingStatusDescription(_ status: ATTrackingManager.AuthorizationStatus = ATTrackingManager.trackingAuthorizationStatus) -> String { | |
switch status { | |
case .notDetermined: | |
return "not determined" | |
case .restricted: | |
return "restricted" | |
case .denied: | |
return "denied" | |
case .authorized: | |
return "authorized" | |
@unknown default: | |
return "unknown" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment