LoginSignup
14
12

More than 3 years have passed since last update.

iOS14で、IDFAオプトインダイアログを出してみる

Last updated at Posted at 2020-08-16

追記:iOS14のIDFAオプトアウト来年まで延期する事になったようです:frowning2:→詳細


現時点で、iOS14から必須のIDFAオプトインダイアログを出す方法に絞ってメモ。

IDFAや業界について詳しく解説して頂いている記事は他に沢山ありますので、
ここでは純粋にIDFAオプトインを表示する点について書きます。

1. まず、iOS14のシミュレータが動くxcode12を入れるため、macOSのバージョンを10.15.4以上にする必要があります。

image.png

2. 続いてxcode12のベータ版をダウンロード(ここではbeta4をダウンロード)

image.png

10G以上ありますので、ダウンロード時間と、macの容量に余裕をもたせる必要あります。

3. IDFAオプトインダイアログを表示するためのサンプルコードと、info.plistを記載

xcode12betaで新規アプリ作成等で、UIViewControllerを用意し、ダイアログ表示のスクリプトを記載

ATTrackingManager.requestTrackingAuthorizationを呼び出す

UIViewController.swift
//
//  ViewController.swift
//  IDFA-optin
//
//  Created by hoge on 2020/08/16.
//

import UIKit

import AdSupport
import AppTrackingTransparency

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        ATTrackingManager.requestTrackingAuthorization(completionHandler: { status in
                    switch status {
                    case .authorized:
                        print("authorized \(ASIdentifierManager.shared().advertisingIdentifier)")
                    case .denied:
                        print("denied")
                    case .restricted:
                        print("restricted")
                    case .notDetermined:
                         print("notDetermined")
                    @unknown default:
                        fatalError()
                    }
                })
    }
}

各ステータスについて

info.plistにダイアログに表示する説明を、NSUserTrackingUsageDescriptionを追記

info.plist
<key>NSUserTrackingUsageDescription</key>
    <string>info.plistの内容!hogehogetest
        ※説明書き等〜〜〜〜〜〜〜〜
        〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜
        〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜
        〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜
        〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜
        abcdefghijklmnopqrstuvwxyz
    </string>

image.png

iOS14のシミュレータが選択されているか確認し、実行

image.png

トラブルシューティング

下記エラーでクラッシュしたら、info.plistにNSUserTrackingUsageDescriptionが正しく設定されていない

2020-08-08 12:07:34.121113+0900 UIActivityViewSample[5854:133563] [access] This app has crashed because it attempted to access privacy-sensitive data without a usage description.  The app's Info.plist must contain an NSUserTrackingUsageDescription key with a string value explaining to the user how the app uses this data.

参考にさせて頂いたサイトさま🙇

https://qiita.com/koooootake/items/e467be2c4f63ff605841
https://qiita.com/yofuru/items/213b88b85553631204e4
https://qiita.com/bosteri_bon/items/d84cd031784c6a1d3eff

14
12
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
14
12