kabochappinote

articlesadMob
adMob in Swift

adMob in Swift


2021-04-11 10:08:33

adMobの設定


プロジェクトルート % pod init
プロジェクトルート % open Podfile -a Xcode  //XcodeでPodfileをオープン


# Podfile
platform :ios, '14.4'

target 'プロジェクト' do
  use_frameworks!

  # Pods for GifCamera
  pod 'Firebase/Core'
  pod 'Google-Mobile-Ads-SDK'

end


post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '9.0'
    end
  end
end



プロジェクトルート % pod install --repo-update


# info.plist
GADIsAdManagerApp : Boolean : true
GADApplicationIdentifier : String : アプリID
SKAdNetworkItems: Array: -
    Item 0 : Dictionary: -
        SKAdNetworkIdentifier : String : cstr6suwn9.skadnetwork


 ※ IDFA利用許可ダイアログ表示用(iOS 14 以降の設定)

# info.plist
Privacy - Tracking Usage Description : String : This identifier will be used to deliver personalized ads to you.


 ※ IDFA確認:


ASIdentifierManager.shared().advertisingIdentifier


// AppDelegate.swift
import UIKit
import Firebase
import GoogleMobileAds
import AppTrackingTransparency
import AdSupport

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(
        _ application: UIApplication, 
        didFinishLaunchingWithOptions launchOptions: 
        [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        FirebaseApp.configure()

        if #available(iOS 14, *) {
            ATTrackingManager.requestTrackingAuthorization(
                completionHandler: { status in
                GADMobileAds.sharedInstance().start(completionHandler: nil)
            })
        } else {
            GADMobileAds.sharedInstance().start(completionHandler: nil)
        }
        return true
    }
}


// ViewController.swift
import UIKit
import GoogleMobileAds

class ViewController: UIViewController, GADBannerViewDelegate {

    var bannerView: GADBannerView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // In this case, we instantiate the banner with desired ad size.
        bannerView = GADBannerView(adSize: kGADAdSizeBanner)
        bannerView.adUnitID = "広告ユニットID"
        bannerView.rootViewController = self
        bannerView.load(GADRequest())

        bannerView.delegate = self
        addBannerViewToView(bannerView)
    }

    func addBannerViewToView(_ bannerView: GADBannerView) {
        bannerView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(bannerView)
        view.addConstraints(
            [NSLayoutConstraint(item: bannerView,
                                attribute: .bottom,
                                relatedBy: .equal,
                                toItem: view.safeAreaLayoutGuide,
                                attribute: .top, //.bottom,
                                multiplier: 1,
                                constant: 0),
            NSLayoutConstraint(item: bannerView,
                                attribute: .centerX,
                                relatedBy: .equal,
                                toItem: view,
                                attribute: .centerX,
                                multiplier: 1,
                                constant: 0) ])
     }
}

GoogleDevelopers(バナー広告説明)
GoogleDevelopers(バナー広告 iOS14以降の準備)

⚪︎ Podの入れ直し

// ターミナルで
rm -rf Pods  // .xcworkspace と Podfile.lock も削除
pod install --repo-update  // 再インストール
back