본문 바로가기

iOS

[Swift] Google Admob 추가

반응형

cocoapod 으로 admob 설치 후.

 

import SnapKit
import GoogleMobileAds

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    GADMobileAds.sharedInstance().start(completionHandler: nil)
    return true
}

private var bannerView: GADBannerView!

override func viewDidLoad() {
    super.viewDidLoad()

    let adSize = GADAdSizeFromCGSize(CGSize(width: view.frame.width, height: 50))
    bannerView = GADBannerView(adSize: adSize)
    bannerView.backgroundColor = .white
    safetyArea.addSubview(bannerView)
    bannerView.snp.makeConstraints {
        $0.width.equalTo(view.frame.width)
        $0.top.equalTo(safetyArea)
    }
    
    bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
    bannerView.rootViewController = self
    bannerView.load(GADRequest())
    bannerView.delegate = self
}

extension vcDevice: GADBannerViewDelegate {
    /// Tells the delegate an ad request loaded an ad.
    func bannerViewDidReceiveAd(_ bannerView: GADBannerView) {
        print("adViewDidReceiveAd")
    }
    
    /// Tells the delegate an ad request failed.
    func bannerView(_ bannerView: GADBannerView,
                didFailToReceiveAdWithError error: Error) {
        print("adView:didFailToReceiveAdWithError: \(error.localizedDescription)")
    }
    
    /// Tells the delegate that a full-screen view will be presented in response
    /// to the user clicking on an ad.
    func bannerViewWillPresentScreen(_ bannerView: GADBannerView) {
        print("adViewWillPresentScreen")
    }
    
    /// Tells the delegate that the full-screen view will be dismissed.
    func bannerViewWillDismissScreen(_ bannerView: GADBannerView) {
        print("adViewWillDismissScreen")
    }
    
    /// Tells the delegate that the full-screen view has been dismissed.
    func bannerViewDidDismissScreen(_ bannerView: GADBannerView) {
        print("adViewDidDismissScreen")
    }
    
    /// Tells the delegate that a user click will open another app (such as
    /// the App Store), backgrounding the current app.
    func bannerViewWillLeaveApplication(_ bannerView: GADBannerView) {
        print("adViewWillLeaveApplication")
    }
}

'iOS' 카테고리의 다른 글

[Swift] characteristic read, write  (0) 2022.01.06
[Swift] contentView - expandable tableView  (0) 2022.01.02
[Swift] modalPresentationStyle  (0) 2021.12.24
[Swift] 작업영역에서 Statusbar 제외하기  (0) 2021.12.15
[Swift] SnapKit  (0) 2021.12.15