4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【iOS15】IDFA許諾ダイアログ表示できなくった件

Last updated at Posted at 2021-10-05

#何をするか?
iOS15以降からIDFA許諾ダイアログを表示する方法が少し変わったのでメモ

#原因

Calls to the API only prompt when the application state is: UIApplicationStateActive. Calls to the API through an app extension do not prompt.

iOS15から新たな制限が加わり、「アプリの状態がActiveの時しか表示しない」ようになった模様。
以下公式ドキュメントになります。
https://developer.apple.com/documentation/apptrackingtransparency/attrackingmanager/3547037-requesttrackingauthorization

#これまでの実装
AppDelegtedidFinishLaunchingWithOptionsでidfaの許諾処理を行なっていたが、ここが呼ばれる時点ではアプリの状態はInactiveなのでダイアログは表示されない。

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        if #available(iOS 14, *) {
            ATTrackingManager.requestTrackingAuthorization(completionHandler: { status in
                GADMobileAds.sharedInstance().start(completionHandler: nil)
            })
        } else {
            // Fallback on earlier versions
            GADMobileAds.sharedInstance().start(completionHandler: nil)
        }
        return true
    }

#対応内容
ScenceDelegatesceneDidBecomeActiveでidfa許諾処理を行うことで対応。
Active状態のはずだが、普通に処理を走らせるだけだとダイアログが表示されないので、処理を1秒遅らせることでidfa許諾ダイアログが表示されるようになりました。

func sceneDidBecomeActive(_ scene: UIScene) {
    DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
        if #available(iOS 14, *) {
            ATTrackingManager.requestTrackingAuthorization(completionHandler: { status in
                GADMobileAds.sharedInstance().start(completionHandler: nil)
            })
        } else {
            GADMobileAds.sharedInstance().start(completionHandler: nil)
        }
    }
}
4
2
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?