LoginSignup
6
1

More than 5 years have passed since last update.

SwiftでAdMobをGDPRに対応させる(PersonalizedAdConsentの簡単な説明)

Last updated at Posted at 2018-05-31

Swiftでの実装方法です。

参考程度に使ってください。
自身のコードを説明書きのところに挿入してください。
実装は自己責任で。

Xcode9.4
Swift 4.1.2
cocoapods 1.5.3

AdMob(サイト)側の設定はこちらの記事を参考にしてください。

導入

Podfileに追加
pod 'PersonalizedAdConsent'

インストール
pod install

実装

import PersonalizedAdConsent
import AdSupport
import GoogleMobileAds

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        if !PACConsentInformation.sharedInstance.isRequestLocationInEEAOrUnknown ||//ユーザーの地域がEEAまたは不明(! で反転
            PACConsentInformation.sharedInstance.consentStatus == .personalized ||  //パーソナライズに同意済み
            PACConsentInformation.sharedInstance.consentStatus == .nonPersonalized {//ノンパーソナライズに同意済み

            //自身のAdMob処理に追加してください
            let admobRequest = GADRequest()
            //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            //追加
            if PACConsentInformation.sharedInstance.consentStatus == .nonPersonalized {

                let extras = GADExtras()
                extras.additionalParameters = ["npa": "1"]//ノンパーソナライズ広告に設定
                admobRequest.register(extras)
            }
            //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        }

        /*テスト時に記述--------------
        PACConsentInformation.sharedInstance.debugIdentifiers = [ASIdentifierManager.shared().advertisingIdentifier.uuidString]//端末のテストIDをセット

        PACConsentInformation.sharedInstance.debugGeography = .EEA //地域をEEAに設定
        //PACConsentInformation.sharedInstance.debugGeography = .notEEA // 地域をEEA圏外に設定
        --------------------------*/

        PACConsentInformation.sharedInstance.requestConsentInfoUpdate(
            forPublisherIdentifiers: ["ADMobのパブリッシャーID"])
        {(_ error: Error?) -> Void in
            if let error = error {
                // Consent info update failed.
            } else {
                // Consent info update succeeded. The shared PACConsentInformation
                // instance has been updated.
            }
        }

        // TODO: Replace with your app's privacy policy url.
        guard let privacyUrl = URL(string: "プライパシーポリシーURL"),
            let form = PACConsentForm(applicationPrivacyPolicyURL: privacyUrl) else {
                print("incorrect privacy URL.")
                return
        }
        form.shouldOfferPersonalizedAds = true //パーソナライズ広告同意ボタンの有無
        form.shouldOfferNonPersonalizedAds = true //ノンパーソナライズ広告同意ボタンの有無
        form.shouldOfferAdFree = true // 有料版アプリボタンの有無

        form.load {(_ error: Error?) -> Void in
            print("Load complete.")
            if let error = error {
                // Handle error.
                print("Error loading form: \(error.localizedDescription)")

            } else {
                // ロード成功
                form.present(from: self) { (error, userPrefersAdFree) in
                    if let error = error {
                        // Handle error.
                    } else if userPrefersAdFree {
                        // 有料版アプリへ誘導処理

                    } else {
                        // ユーザーがどれに同意したか

                        let status =
                            PACConsentInformation.sharedInstance.consentStatus

                        switch status {
                        case .personalized:
                            // パーソナライズ広告
                            break
                        case .nonPersonalized:
                            //ノンパーソナライズ広告
                            //上部に示した広告設定を忘れずに
                            break

                        case .unknown:
                            //不明なステータス
                            break
                        }
                    }
                }
            }
        }
    }
6
1
2

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
6
1