0
1

More than 1 year has passed since last update.

【SwiftUI】Appからのトラッキング要求

Last updated at Posted at 2021-08-05

###メモ
・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>

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1