###メモ
・Appからのトラッキング要求は最初の実行時のみポップアップが表示される。
・再インストールなどすれば再びポップアップが表示
・設定のアプリへの権限許可?画面で設定変更も可能
トラッキングの許可を要求したいタイミングで"requestPermisson()"を実行
上記の通り、最初だけポップアップが表示され、以後表示されなくなる。
###コード
ContentView.swift
struct ContentView: View {
var body: some View {
Button(action: {
requestPermission()
}){
Text("広告許可")
}
//トラキング許可メソッド
func requestPermission() {
if #available(iOS 14, *) {
ATTrackingManager.requestTrackingAuthorization { 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")
}
}
}
}
【追記】
info.plistへ追記も必要でした。これがないとクラッシュしますね。
<Key>Privacy - Tracking Usage Description<Key>
<Value>広告を最適化するためにトラッキング許可してください<Value>